php 如何通过laravel eloquent模型连接三个表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29165410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to join three table by laravel eloquent model
提问by Nay
I have three table
我有三张桌子
Articles table
文章表
id
title
body
categories_id
user_id
Categories table
类别表
id
category_name
User table
用户表
id
user_name
user_type
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!
我想用他们的类别名称而不是 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();
But I want to do by Eloquent way. Please, how could I do?
但我想通过 Eloquent 的方式来做。请问,我该怎么办?
回答by Anand Patel
With Eloquent its very easy to retrieve relational data. Checkout the following example with your scenario in Laravel 5.
使用 Eloquent 可以很容易地检索关系数据。使用 Laravel 5 中的场景查看以下示例。
We have three models:
我们有三种型号:
1) Article (belongs to user and category)
1)文章(属于用户和类别)
2) Category (has many articles)
2)分类(有很多文章)
3) User (has many articles)
3)用户(有很多文章)
1) Article.php
1) 文章.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) 类别.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) 用户.php
<?php
namespace App\Models;
use Eloquent;
class User extends Eloquent
{
protected $table = 'users';
public function articles()
{
return $this->hasMany('App\Models\Article');
}
}
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.
您需要了解模型中的数据库关系和设置。用户有很多文章。类别有很多文章。文章属于用户和类别。一旦你在 Laravel 中建立了关系,检索相关信息就变得很容易了。
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();
You can learn more at http://laravel.com/docs/5.0/eloquent
您可以在http://laravel.com/docs/5.0/eloquent了解更多信息
回答by Nay Zaw Oo
Try:
尝试:
$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();
回答by Akshay Kashyap
$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]);