【问题标题】:laravel - Query Builder vs. Eloquentlaravel - 查询生成器与 Eloquent
【发布时间】:2014-05-13 21:15:52
【问题描述】:

我有这个声明,它工作正常,但没有我想要的那么好:

        $recipes = DB::table('recipes')
            ->join('category_recipe', 'recipes.id', '=', 'category_recipe.recipe_id')
            ->join('category', 'category.id', '=', 'category_recipe.category_id')
            ->join('users', 'users.id', '=', 'recipes.user_id')
            ->where('category.id', '=', $cat_id)->get(array('recipes.*','users.*'));  

如何将其翻译成 Eloquent?

为什么?
我想为多种方法使用一个视图。
这个视图或 foreach 看起来像这样:

        @foreach($recipes as $recipe)
        {{link_to_route('recipe_details',$recipe->title,array('id'=>$recipe->id))}} - By {{ $recipe->user->firstname }}  - Category: @foreach($recipe->category as $cat) {{ $cat->title }} @endforeach </br>
    @endforeach  

如您所见,我使用的是“用户”关系。显然,这个 foreach 不适用于顶部的 Query,因为没有“用户”模型。

那么如何将 Query 翻译成 Eloquent?
我试过了

$recipes = Recipe::with('category')->where('category_id',$cat_id)->get();  

但这不起作用。请问有什么提示吗?

这是我的模型:
食谱.php

    public function user() {
        return $this->belongsTo('User','user_id');
}
public function category() {
        return $this->belongsToMany('Category','category_recipe');
}  

Category.php

    public function recipes() {
    return $this->belongsToMany('Recipe');
}  

用户.php

    public function recipes() {
        return $this->hasMany('Recipe','user_id');
}  

谢谢!

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    你可以试试这个:

    $recipes = Recipe::with(array('user', 'categories' => function($q) use ($cat_id) {
        $q->where('id', $cat_id);
    }))->get();
    

    以下变化:

    public function category() {
        return $this->belongsToMany('Category','category_recipe');
    }
    

    收件人(category 应该是 categories in Recipe.php):

    public function categories() {
        return $this->belongsToMany('Category','category_recipe');
    }
    

    顺便说一句,你也可以像这样使用join(使用Eloquent模型,如果你需要的话):

    Recipe::join('category_recipe', 'recipes.id', '=', 'category_recipe.recipe_id')
          ->join('category', 'category.id', '=', 'category_recipe.category_id')
          ->join('users', 'users.id', '=', 'recipes.user_id')
          ->where('category.id', '=', $cat_id)->get();
    

    更新:你也可以试试这个:

    $recipes = Recipe::whereHas('categories', function($q) use ($cat_id) {
        $q->where('id', $cat_id);
    })->with('user')->get();
    

    【讨论】:

    • 谢谢。会试试看。我尝试了 join 方法,但这仍然不能使 §recipes->user->firstname 工作,我需要它来使用其他方法。但是好的,我试试你的第一个建议! :)
    • §recipes-&gt;user-&gt;firstname 不起作用,因为get() 返回一个集合,因此您需要使用first() 来获取第一个项目,如果有多个项目,那么您可以使用循环。尝试,§recipes-&gt;first()-&gt;user-&gt;firstname 第一个。
    • 您的版本似乎可以工作,但我无法正确执行“where”。它向我显示了所有行,而不是(在这种情况下)只有两个类别 ID = 6。这是我的代码:pastebin.com/AnEuXRfc
    • 如果您只想获取带有类别的行,那么它应该是使用 whereHas 的不同方法。
    • 您也可以添加-&gt;where('category.id', $cat_id)-&gt;get()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2017-08-31
    • 2018-05-20
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2020-07-09
    相关资源
    最近更新 更多