我试图让所有模块在经理中定位相同的内容。
AdminController:
$this->manager = Manager::where('id',$this->id_manager)->with('fields')->first();
$managerSiblings = Module::with(array('managers' => function($query){
$query->where('content',$this->manager->content);
}))->whereHas('managers', function($query){
$query->where('content',$this->manager->content);
})->get();
ErrorException in Module.php line 36:
Undefined property: Illuminate\Database\Eloquent\Builder::$manager
AdminController:
$this->manager = Manager::where('id',$this->id_manager)->with('fields')->first();
$manager = Module::with('managerSiblings')->get();
模块:
class Module extends Model {
...
public function managers(){
return $this->hasMany('App\Manager','id_module')->orderBy('order');
}
public function managerSiblings( $q ){
return $q->with(array('managers' => function($query) use($q){
$query->where('content',$q->manager->content);
}))->whereHas('managers', function($query) use($q){
$query->where('content',$q->manager->content);
});
}
}
问题在于$q->manager->content
,它是AdminController的一个属性。我不知道如何从Module Class访问该属性。
答案 0 :(得分:0)
您可以将参数传递给这样的关系:
public function managerSiblings($content){
return $this->hasMany('App\Manager','id_module')->where('content', $content)->orderBy('order');
}
然后在你的控制器中调用它时,你只需记得像这样调用get函数:
$siblingManagers = Module::has('managers')->siblingManagers($this->manager->content)->get();
Haven未对此代码进行测试,因此您可能需要仔细检查所有内容。