如何通过Laravel雄辩的模型加入三张桌子 [英] How to join three table by laravel eloquent model

查看:50
本文介绍了如何通过Laravel雄辩的模型加入三张桌子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个桌子

 id
 title
 body
 categories_id
 user_id

类别表

  id
  category_name

用户表

 id
 user_name
 user_type

我想显示其类别名称(而不是category_id)和用户名(而不是user_id)的文章 我尝试像这些查询一样工作!

I want to show articles with their category name instead of category_id and user_name instead of user_id I try like these query It is work!

$articles =DB::table('articles')
                ->join('categories', 'articles.id', '=', 'categories.id')
                ->join('users', 'users.id', '=', 'articles.user_id')
                ->select('articles.id','articles.title','articles.body','users.username', 'category.name')
                ->get();

但是我想用口才来做.拜托,我该怎么办?

But I want to do by Eloquent way. Please, how could I do?

推荐答案

使用Eloquent,它非常容易检索关系数据.在Laravel 5中查看以下示例与您的场景.

With Eloquent its very easy to retrieve relational data. Checkout the following example with your scenario in Laravel 5.

我们有三种模型:

1)文章(属于用户和类别)

1) Article (belongs to user and category)

2)类别(有很多文章)

2) Category (has many articles)

3)用户(有很多文章)

3) User (has many articles)

1)Article.php

1) Article.php

<?php

namespace App\Models;
 use Eloquent;

class Article extends Eloquent{

    protected $table = 'articles';

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\Category');
    }

}


2)Category.php


2) Category.php

<?php

namespace App\Models;

use Eloquent;

class Category extends Eloquent
{
    protected $table = "categories";

    public function articles()
    {
        return $this->hasMany('App\Models\Article');
    }

}


3)User.php


3) User.php

<?php

namespace App\Models;
use Eloquent;

class User extends Eloquent
{
    protected $table = 'users';

    public function articles()
    {
        return $this->hasMany('App\Models\Article');
    }

}

您需要了解您的数据库关系和模型中的设置.用户有很多文章.类别有很多文章.文章属于用户和类别.一旦在Laravel中设置了关系,就可以轻松检索相关信息.

You need to understand your database relation and setup in models. User has many articles. Category has many articles. Articles belong to user and category. Once you setup the relationships in Laravel, it becomes easy to retrieve the related information.

例如,如果要使用用户和类别检索文章,则需要编写:

For example, if you want to retrieve an article by using the user and category, you would need to write:

$article = \App\Models\Article::with(['user','category'])->first();

,您可以像这样使用它:

and you can use this like so:

//retrieve user name 
$article->user->user_name  

//retrieve category name 
$article->category->category_name

在另一种情况下,您可能需要检索类别中的所有文章,或检索特定用户的所有文章.您可以这样写:

In another case, you might need to retrieve all the articles within a category, or retrieve all of a specific user`s articles. You can write it like this:

$categories = \App\Models\Category::with('articles')->get();

$users = \App\Models\Category::with('users')->get();

您可以在 http://laravel.com/docs/5.0/eloquent 上了解更多信息.

You can learn more at http://laravel.com/docs/5.0/eloquent

这篇关于如何通过Laravel雄辩的模型加入三张桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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