id
如果过滤器的冲突字段替换了模型id
字段并且$this->id
在计算字段中来自另一个表,而不是来自模型,则存在相同的问题。所以解决方案是->select([...])
从具有别名的模型中重复查询(相同!)值。例如(使用别名求解user.id
字段)userid
模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class User extends Model
{
//...
public function scopeMyScope($query, int $userId) : bool
{
return $query
->select('user.id', 'user.id as userid'/*,...*/)
->[...];
}
//...
筛选
<?php
namespace App\Nova\Filters;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Laravel\Nova\Filters\Filter;
class MyFilter extends Filter
{
//...
public function apply(Request $request, $query, $value)
{
//Same select fields as in model with userid, dont skip select in the filter!
return $query
->select('user.id', 'user.id as userid'/*,...*/)
->[...];
}
//...
资源
<?php
namespace App\Nova;
use App\Nova\Filters\MyFilter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
class User extends \App\Nova
{
//...
public function fields(Request $request)
{
return [
//userid
Boolean::make('some', function () {
return \App\Models\User::myScope($this->userid);
}),
];
}
//...
}