sql 查询中的不明确字段

Ambiguous field in sql query

我在尝试按 ID 过滤查询时遇到以下错误:

My query:

 $film=film::wherehas('actor', function($q) use($actor_id){
        $q->where('actor_id','=', $actor_id);
      })->get();

model Film:

class film extends Model{
    protected $table = "film";  
    protected $primaryKey = 'film_id';
    public $timestamps = false;
    use HasFactory;
    protected $sql=['film_id', 'title', 'length'];
}

您的查询不知道您在该 where() 子句中针对哪个 actor_id 列。附加 table 名称,例如:

$q->where('film_actor.actor_id', '=', $actorId);
// or `actors.actor_id`, not sure which is being expected here.

或者考虑将 actors table 的 actor_id 更改为仅 id。通常,您不会将 table 名称附加到 id 作为主键。与 films.film_id 相同;那可以只是 id.

只需将您的查询更改为:

$film=film::wherehas('actor', function($q) use($actor_id){
    $q->where('film_actor.actor_id','=', $actor_id);
})->get();