[英]Local scopes in Laravel Model didn't work
I created new scope in Laravel's model and it didn't work at all.我在 Laravel 的 model 中创建了新的 scope,但它根本不起作用。 It seems invisible for the controller.
controller 似乎不可见。
File test.php in Models:型号中的文件 test.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
public function scopeLatest(Builder $query, int $limit)
{
return $query
->select(['id', 'title', 'main_photo', 'area', 'price', 'city', 'short_desc'])
->whereIn('status', ['Dostepne', 'Zarezerwowane'])
->orderBy('id', 'desc')
->limit($limit)
;
}
}
Controller file: Controller 文件:
$flats = Test::latest(3)->get();
In the end, I'm reciving every record in database.最后,我正在接收数据库中的每条记录。 When I delete get methode in controller, i'm recive no records.
当我删除 controller 中的get方法时,我没有收到任何记录。 Whatever I write in scopeLatest it doesn't matter.
无论我在scopeLatest中写什么都没关系。
There is already a method named latest
that exists on the Eloquent Query Builder; Eloquent 查询生成器上已经存在一个名为
latest
的方法; it is a public method so that is what you are calling directly.它是一种公共方法,因此您可以直接调用它。 To be able to apply a scope (call a scope on the Builder) you would have to be calling a non existing or inaccessible (not visible) method which would make PHP call the
__call
method which would eventually be checking if a scope exists and then calling it. To be able to apply a scope (call a scope on the Builder) you would have to be calling a non existing or inaccessible (not visible) method which would make PHP call the
__call
method which would eventually be checking if a scope exists and then调用它。
Name your scope something else if you want to be able to apply it in the fluent fashion.如果您希望能够以流畅的方式应用它,请将您的 scope 命名为其他名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.