Laravel使用'With'子句将参数从控制器传递到模型 [英] Laravel pass parameters from controller to Model using 'With' clause

查看:170
本文介绍了Laravel使用'With'子句将参数从控制器传递到模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新朋友, 我想使用with子句

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

我的模特

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_id的值,但是如果我使用它,它将显示正确的jSON

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

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

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

推荐答案

您的childrenRecursive完全没有错.

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

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

我无法对其进行测试,因此可能无法100%工作

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

这篇关于Laravel使用'With'子句将参数从控制器传递到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆