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

浪尽此生 提交于 2020-01-25 16:40:10

问题


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

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_id in my child array, but if i use this it shows the correct 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?


回答1:


Your childrenRecursive isn't wrong at all.

See here an simliar example: 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%




回答2:


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


来源:https://stackoverflow.com/questions/39122568/laravel-pass-parameters-from-controller-to-model-using-with-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!