我有三个模型如下:
class Category extends Eloquent
{
protected $primaryKey = 'categoryID';
public $timestamps = false;
protected $fillable = [
'categoryName',
];
public function categoriesfields()
{
return $this->hasMany(\App\Models\Categoriesfield::class, 'categoryID');
}
}
Categoriesfield:
class Categoriesfield extends Eloquent
{
protected $primaryKey = 'fieldID';
public $timestamps = false;
protected $casts = [
'categoryID' => 'int'
];
protected $fillable = [
'fieldName_ar',
'categoryID'
];
public function category()
{
return $this->belongsTo(\App\Models\Category::class, 'categoryID');
}
public function categoriesoptions()
{
return $this->hasMany(\App\Models\Categoriesoption::class, 'fieldID');
}
}
Categoriesoption:
class Categoriesoption extends Eloquent
{
protected $primaryKey = 'optionID';
public $timestamps = false;
protected $casts = [
'optionParent' => 'int',
'fieldID' => 'int'
];
protected $fillable = [
'fieldID'
];
public function categoriesfield()
{
return $this->belongsTo(\App\Models\Categoriesfield::class, 'fieldID');
}
}
当我运行此查询时出现此错误:
$data= Category::with('category.categoriesfields')-
>with('category.categoriesfields.categoriesoptions')
->where('fieldID', '=', 3)->get();
SQLSTATE [42S22]:未找到列:1054未知列' fieldID'在' where子句' (SQL:select * from categories
其中fieldID
= 3)
任何帮助请知道问题是什么!!?
答案 0 :(得分:0)
出于此目的,请参阅Constraining Eager Loads:
$fieldID = 3; // added the variable here to see how to import it to the closure
$data = Category::with([
'categoriesfields' => function ($query) use ($fieldID) {
$query->with('categoriesoptions')->where('fieldID', '=', $fieldID);
}
])->get();
之所以发生这种情况,是因为在没有fieldID
列的类别表中添加了where子句。