我目前的模特结构:
Image.php
public function imageable()
{
return $this->morphTo();
}
User.php
public function images()
{
return $this->morphMany(Image::class, 'imageable');
}
我这样查询:
$gender = 1;
$lookIds = Image::whereHas('imageable' , function($query) use($gender){
$query->where('gender', $gender);
})->get();
它归还给我:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'gender' in 'where clause
任何建议或更好的解决方案,以获得性别为0的用户中的所有图片?
答案 0 :(得分:0)
尝试使用:
$gender = 1;
$lookIds = Image::whereHas('users', function($query) use ($gender) {
$query->where('gender',$gender);
})->get();
我假设您的用户表有性别字段?