I have three models like following:
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');
}
}
when I run this query I got this error :
$data= Category::with('category.categoriesfields')-
>with('category.categoriesfields.categoriesoptions')
->where('fieldID', '=', 3)->get();
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fieldID' in 'where clause' (SQL: select * from categories
where fieldID
= 3) Any help please to know what is the problem !!?
See Constraining Eager Loads for this purpose:
$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();
It happends because the where clause is added on the categories table which does not have the fieldID
column.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.