表:
categories
--id
materials
--id
category_material
--id
--category_id
--material_id型号:
class Material extends Model {
public function categories(){
return $this->belongsToMany('App\Category');
}
}
class Category extends Model {
public function materials(){
return $this->belongsToMany('App\Material');
}
}我需要"category_id = 1“的所有材料
试着:
$category_id = '1';
$materials = Material::with('categories')->where('category_id', $category_id)->get();未知列'materials.category_id'
$category_id = '1';
$materials = Material::with(['categories',function($query) use ($category_id){
$query->where('category_id', $category_id);
}])->get();ErrorException在Builder.php第792行:Builder.php()期望参数2是字符串,对象给定
帮帮我求你了。
发布于 2015-12-02 10:55:45
传递给with的数组应该是relation_name => closure格式的
$category_id = '1';
$materials = Material::whereHas('categories', function($query) use ($category_id){
$query->where('category_id', $category_id);
})->get();https://stackoverflow.com/questions/34038432
复制相似问题