Laravel 使用“With”子句将参数从控制器传递到模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39122568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:21:43  来源:igfitidea点击:

Laravel pass parameters from controller to Model using 'With' clause

phplaravellaravel-5eloquentrecursive-query

提问by Xabir

I am new in Laravel, I want to pass the the $id from my controller in the model using with clause

我是 Laravel 的新手,我想使用 with 子句在模型中传递来自我的控制器的 $id

My model

我的模型

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');
  }
}

My Controller

我的控制器

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

My current output is

我目前的输出是

[
  {
    "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": []
      }
    ]
  }
]

I passed $id=12, but the problem is I get the values of others restaurant_idin my child array, but if i use this it shows the correct jSON

我通过了$id=12,但问题是我得到了restaurant_id子数组中其他人的值 ,但是如果我使用它,它会显示正确的jSON

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

my questions is how can i pass the $id from the controller to the model or is there any other approach?

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

回答by lippoliv

Your childrenRecursiveisn't wrong at all.

你的childrenRecursive一点都没有错。

See here an simliar example: https://stackoverflow.com/a/18600698/2160816

在这里看到一个类似的例子:https://stackoverflow.com/a/18600698/2160816

So I think this should work

所以我认为这应该有效

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

Your Controller then could call

然后你的控制器可以调用

public function show($id)
{
    $menucatagories = Menucategory::where('parent_id','0')->childrenRecursive(12)->get();
    return $menucatagories;
}

I could not test it so may it won't work 100%

我无法测试它所以它可能不会 100% 工作

回答by Hamza Dairywala

You can ass your parameter in controller itself in the following way.

您可以通过以下方式在控制器本身中分配参数。

     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;
      }