【问题标题】:How to join three table by laravel eloquent model如何通过 laravel eloquent 模型连接三个表
【发布时间】:2015-05-23 19:06:39
【问题描述】:

我有三张桌子

文章表

 id
 title
 body
 categories_id
 user_id

类别表

  id
  category_name

用户表

 id
 user_name
 user_type

我想显示文章的类别名称而不是 category_id 和 user_name 而不是 user_id 我尝试像这些查询它是工作!

$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();

但我想用 Eloquent 的方式来做。请问,我该怎么办?

【问题讨论】:

  • 如何创建一个连接这些表的视图,然后创建只从中读取的 Eloquent 模型?代码复杂性降低,您获利,无需付出任何努力。
  • 所有其他相关的模型和功能都与 Eloquent 模型一起工作。所以我需要使用 Eloquent Way 。
  • 你误会了吗:->join('categories', 'articles.id', '=', 'categories.id')?而不是articles.id,它应该是articles.categories_id。还是我错了?

标签: php mysql laravel


【解决方案1】:

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

我们有三种型号:

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

  2. 分类(有很多文章)

  3. 用户(有很多文章)


  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');
        }
    }
  1. Category.php
    <?php
    namespace App\Models;
    
    use Eloquent;
    
    class Category extends Eloquent {
        protected $table = "categories";
    
        public function articles() {
            return $this->hasMany('App\Models\Article');
        }
    }
  1. 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 中设置好关系后,检索相关信息就很容易了。

例如,如果您想通过使用用户和类别来检索文章,您需要编写:

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

你可以这样使用:

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

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

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

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

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

【讨论】:

  • 感谢您的回复但我使用 laravel 4 并希望以 json 格式返回数据。像这些 return Response::json( array( 'total_count' =&gt; $count, 'incomplete_results' =&gt; false, 'items'=&gt; $articles-&gt;toArray() )); json 格式我该怎么办?
  • 无论所有代码在 Laravel 4 中都能正常工作,你只需要删除命名空间,$articles->toArray() 也一样,请尝试
  • 谢谢兄弟,这是工作,你救了我的命:)。抱歉回复晚了,因为昨天我不在电脑旁边。
  • 我不确定,但似乎这种方式(使用 eloquent)会生成 3 个查询。而带有自定义join 的查询将是单一的。所以下面@NayZawOo 的回答比较容易接受。
  • 如果我不想像$article->category->category_name那样访问它,而是使用像$article->categoryName这样的别名,在这种情况下我该如何编写雄辩的查询?
【解决方案2】:
$articles =DB::table('articles')
                ->join('categories','articles.id', '=', 'categories.id')
                ->join('user', 'articles.user_id', '=', 'user.id')
                ->select('articles.id','articles.title','articles.body','user.user_name', 'categories.category_name')
                ->get();
return view('myarticlesview',['articles'=>$articles]);

【讨论】:

  • 上述查询存在完整性约束违规错误,已在此答案中解决
【解决方案3】:

试试:

$articles = DB::table('articles')
            ->select('articles.id as articles_id', ..... )
            ->join('categories', 'articles.categories_id', '=', 'categories.id')
            ->join('users', 'articles.user_id', '=', 'user.id')

            ->get();

【讨论】:

  • 我想要 json 对象格式返回。因为我需要尝试 $articles->count() 和 $articles->offset($offset); $articles->get(array('id','title','body'));但是你的方式不能应用那些对象函数。感谢您的支持。
  • 返回响应:json($articles);
  • 虽然这不是一种雄辩的方式,但如果您按外部表中的字段进行排序,这是唯一的方式。 (我不推荐,但偶尔需要)。因此,我认为这仍然是一个非常有效的答案。 (问题询问如何加入表,雄辩的方式实际上从未在原始查询中加入它们。)
  • 这不是问题所问的雄辩风格
猜你喜欢
  • 2019-10-04
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多