Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用';与';条款_Php_Laravel_Laravel 5_Eloquent_Recursive Query - Fatal编程技术网

Php 使用';与';条款

Php 使用';与';条款,php,laravel,laravel-5,eloquent,recursive-query,Php,Laravel,Laravel 5,Eloquent,Recursive Query,我是新来的拉维尔人, 我想使用with子句从模型中的控制器传递$id 我的模型 class Menucategory extends Model { protected $fillable = ['title', 'parent_id', 'restaurant_id']; // loads only direct children - 1 level public function children() { return $this->hasMany('App\

我是新来的拉维尔人, 我想使用with子句从模型中的控制器传递$id

我的模型

class Menucategory extends Model
{
  protected $fillable = ['title', 'parent_id', 'restaurant_id'];

  // loads only direct children - 1 level
  public function children()
  {
    return $this->hasMany('App\Menucategory', 'parent_id');
  }

  // recursive, loads all descendants
  public function childrenRecursive()
  {
    return $this->children()->with('childrenRecursive');
  }
}
我的控制器

public function show($id)
{
    $menucatagories = Menucategory::with('childrenRecursive')->where('restaurant_id',$id)->where('parent_id','0')->get();
    return $menucatagories;
}
我当前的输出是

[
  {
    "id": 1,
    "title": "TestMenu Parant",
    "parent_id": 0,
    "restaurant_id": 12,
    "children_recursive": [
      {
        "id": 2,
        "title": "TestMenu SubCat1",
        "parent_id": 1,
        "restaurant_id": 12,
        "children_recursive": [
          {
            "id": 6,
            "title": "TestMenu other sub cat",
            "parent_id": 2,
            *******************
            "restaurant_id": 13,
            *******************
            "children_recursive": []
          },
          {
            "id": 7,
            "title": "TestMenu other sub cat",
            "parent_id": 2,
            "restaurant_id": 12,
            "children_recursive": []
          }
        ]
      },
      {
        "id": 3,
        "title": "TestMenu SubCat2",
        "parent_id": 1,
        "restaurant_id": 12,
        "children_recursive": []
      }
    ]
  }
]
我通过了
$id=12
,但问题是我在我的子数组中获得了其他
restaurant\u id
的值,但如果我使用它,它将显示正确的
jSON

public function childrenRecursive()
{
   $id=12;    
   return $this->children()->with('childrenRecursive')->where('restaurant_id',$id);
}

我的问题是如何将$id从控制器传递到模型,或者是否有其他方法

您可以通过以下方式在控制器本身中设置参数

     public function show($id)
     {
        $menucatagories =Menucategory::with(array('childrenRecursive'=>function($query) use ($id){
         $query->select()->where('restaurant_id',$id);
        }))
        ->where('restaurant_id',$id)->where('parent_id','0')->get();
        return $menucatagories;
      }

你的
孩子递归
一点也没错

这里有一个简单的例子:

所以我认为这应该行得通

public function childrenRecursive($id = 12){
return $this->children()->where('restaurant_id',$id)->with('childrenRecursive');
}
然后,您的控制器可以调用

public function show($id)
{
    $menucatagories = Menucategory::where('parent_id','0')->childrenRecursive(12)->get();
    return $menucatagories;
}
我无法测试它,所以它可能无法100%工作