Laravel 4有许多WHERE子句
不确定这是否是向hasMany参数添加额外查询的正确方法,但未成功。这可能吗Laravel 4有许多WHERE子句,laravel,laravel-4,Laravel,Laravel 4,不确定这是否是向hasMany参数添加额外查询的正确方法,但未成功。这可能吗 public function menuItems($parent=false){ if($parent){ $menuItems = $this->hasMany('MenuItem')->where('parent',$parent); }else{ $menuItems = $this->hasMany('MenuItem'); }
public function menuItems($parent=false){
if($parent){
$menuItems = $this->hasMany('MenuItem')->where('parent',$parent);
}else{
$menuItems = $this->hasMany('MenuItem');
}
return $menuItems;
}
当使用
$menu_items = $menu->menuItems(0);
当传递给父级时,这似乎只是返回一个空数组。即使存在MenuItem->parent=0的数据
我是否需要以某种方式区分我请求的是链接项“parent”而不是主模型“parent”我不确定查询部分,但一开始向函数传递0时是否仍将$parent变量注册为false?因此,也许只需检查$parent是否为null
public function menuItems($parent = null){
if(!$parent == null)){
$menuItems = $this->hasMany('MenuItem')->where('parent',$parent);
}else{
$menuItems = $this->hasMany('MenuItem');
}
return $menuItems;
}
打电话给
$menu_items = $menu->menuItems()->where('parent', 0)->get();
在PHP
0=FALSE
中,更改此选项
if( $parent ){
为此
if( $parent !== false ){
你能发布你的表格布局吗?我不知道这里发生了什么。。子表只能有一个对父行的引用。。您的
->hasMany()。您可能需要提供定义父子关系键的第二个参数。在HasMany类或其父类中找不到where()方法。有人知道调用哪个where()方法吗?我很好奇,因为PHPStorm警告where方法不存在;
if( $parent !== false ){