根据official文档:
The findOrFail and firstOrFail methods will retrieve the first result of the query.
很酷。我想要相反的东西。
假设我有一个包含许多列的表,其中多行可以具有相同的 type_id
并且可以属于相同的 person_id
。
我显然可以创建queryScope 或原始查询 但也许您知道类似lastOrFail
的东西?
简单地说,它会从所有满足特定条件的行中返回最近的行。像这样的东西:
$model = $this->makeModel()
->where('person_id', '=', $personId)
->where('type_id', '=', $typeId)
->lastOrFail();
您是否知道类似的事情,或者您是否认为这应该由 Taylor 在 Laravel 中本地实现?
最佳答案
如果您以相反的顺序排列项目,firstOrFail
将给您最后一项:
$query = Model::where('person_id', $personId)->where('type_id', $typeId);
$model = $query->latest()->firstOrFail();
关于php - Laravel 5.1 - 给我最近的行 - lastOrFail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33793547/