多重连接laravel php
我有一个博客应用程序,它有三个表,我想查看任何搜索查询 三个表格:多重连接laravel php,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个博客应用程序,它有三个表,我想查看任何搜索查询 三个表格:帖子,类别,标签 型号: 帖子: class Post extends Model { public function category(){ return $this->belongsTo('App\Category'); } public function tags(){ return $this->belongsToMany('App\Tag','post
帖子
,类别
,标签
型号:
帖子:
class Post extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
public function tags(){
return $this->belongsToMany('App\Tag','post_tag','post_id','tag_id');
}
public function users(){
return $this->belongsTo('App\User','author_id');
}
}
class Tag extends Model
{
public function posts(){
return $this->belongsToMany('App\Post','post_tag','tag_id','post_id');
}
}
class Category extends Model
{
protected $table='categories';
public function posts(){
return $this->hasMany('App\Post');
}
}
标签:
class Post extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
public function tags(){
return $this->belongsToMany('App\Tag','post_tag','post_id','tag_id');
}
public function users(){
return $this->belongsTo('App\User','author_id');
}
}
class Tag extends Model
{
public function posts(){
return $this->belongsToMany('App\Post','post_tag','tag_id','post_id');
}
}
class Category extends Model
{
protected $table='categories';
public function posts(){
return $this->hasMany('App\Post');
}
}
类别:
class Post extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
public function tags(){
return $this->belongsToMany('App\Tag','post_tag','post_id','tag_id');
}
public function users(){
return $this->belongsTo('App\User','author_id');
}
}
class Tag extends Model
{
public function posts(){
return $this->belongsToMany('App\Post','post_tag','tag_id','post_id');
}
}
class Category extends Model
{
protected $table='categories';
public function posts(){
return $this->hasMany('App\Post');
}
}
表格结构
/*Table: posts*/
----------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
----------- ---------------- --------------- ------ ------ ------- -------------- ------------------------------- ---------
id int(10) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
created_at timestamp (NULL) YES (NULL) select,insert,update,references
updated_at timestamp (NULL) YES (NULL) select,insert,update,references
title varchar(255) utf8_unicode_ci NO (NULL) select,insert,update,references
body text utf8_unicode_ci NO (NULL) select,insert,update,references
slug varchar(255) utf8_unicode_ci NO UNI (NULL) select,insert,update,references
category_id int(10) unsigned (NULL) YES (NULL) select,insert,update,references
image_path varchar(255) utf8_unicode_ci YES (NULL) select,insert,update,references
author_id int(10) unsigned (NULL) NO MUL (NULL) select,insert,update,references
/*Table: tags*/
---------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
---------- ---------------- --------------- ------ ------ ------- -------------- ------------------------------- ---------
id int(10) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
name varchar(255) utf8_unicode_ci NO (NULL) select,insert,update,references
created_at timestamp (NULL) YES (NULL) select,insert,update,references
updated_at timestamp (NULL) YES (NULL) select,insert,update,references
/*Table: categories*/
---------------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
---------- ---------------- --------------- ------ ------ ------- -------------- ------------------------------- ---------
id int(10) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
name varchar(255) utf8_unicode_ci NO (NULL) select,insert,update,references
created_at timestamp (NULL) YES (NULL) select,insert,update,references
updated_at timestamp (NULL) YES (NULL) select,insert,update,references
/*Table: post_tag*/
-------------------
/*Column Information*/
----------------------
Field Type Collation Null Key Default Extra Privileges Comment
------- ---------------- --------- ------ ------ ------- -------------- ------------------------------- ---------
id int(10) unsigned (NULL) NO PRI (NULL) auto_increment select,insert,update,references
post_id int(10) unsigned (NULL) NO MUL (NULL) select,insert,update,references
tag_id int(10) unsigned (NULL) NO MUL (NULL) select,insert,update,references
我试图寻找的是任何搜索查询,假设他输入的甜甜圈
应该与文章标题
或类别名称
或标签名称
匹配,如果任何匹配发生在与之相关的文章中,应该显示在搜索结果中
我尝试的
$post = DB::table('posts')
->join('categories', 'posts.category_id', 'categories.id')
->join('blog_users', 'blog_users.id', 'posts.author_id')
->join('post_tag', 'posts.id', 'post_tag.post_id')
->join('tags', 'tags.id', 'post_tag.tag_id')
->select(
'posts.*', 'blog_users.name',
'post_tag.tag_id', 'post_tag.post_id',
'tags.name', 'categories.name')
->where(function($query) use ($search) {
$query->where('posts.title', 'LIKE', $search)
->orWhere('categories.name', 'LIKE', $search)
->orWhere('tags.name', 'LIKE', $search);
})
->paginate(5);
但这似乎给了我重复的结果,效果不好
我想知道这个问题的更好解决方案吗?当我发现自己在查询中使用多个联接时(特别是在
选择
查询中),我通常要做的是实现一个数据库视图
来封装查询背后的逻辑
像这样创建迁移
DB::statement("DROP VIEW IF EXISTS view_post");
DB::statement("
CREATE VIEW view_post
AS
SELECT
'posts.*',
'blog_users.name AS blog_users_name',
'post_tag.tag_id',
'post_tag.post_id',
'tags.name AS tags_name',
'categories.name AS categories_name'
FROM posts
INNER JOIN categories
ON (posts.category_id = categories.id)
INNER JOIN blog_users
ON (blog_users.id = posts.author_id)
INNER JOIN post_tag
ON (posts.id = post_tag.post_id)
INNER JOIN tags
ON (tags.id = post_tag.tag_id)
");
class PostView extends Post { // <- To inherit properties of POST to here
protected $table = 'view_post';
}
然后做一个这样的模型
DB::statement("DROP VIEW IF EXISTS view_post");
DB::statement("
CREATE VIEW view_post
AS
SELECT
'posts.*',
'blog_users.name AS blog_users_name',
'post_tag.tag_id',
'post_tag.post_id',
'tags.name AS tags_name',
'categories.name AS categories_name'
FROM posts
INNER JOIN categories
ON (posts.category_id = categories.id)
INNER JOIN blog_users
ON (blog_users.id = posts.author_id)
INNER JOIN post_tag
ON (posts.id = post_tag.post_id)
INNER JOIN tags
ON (tags.id = post_tag.tag_id)
");
class PostView extends Post { // <- To inherit properties of POST to here
protected $table = 'view_post';
}
重要的是要记住,这一切都取决于形势。
对于我的电台来说,这是一个更干净的方法,而不是把我的问题弄得一团糟。我认为你也可以采用这种方法。谢谢
注意:您可能需要进一步修改
视图
和查询
,以表示您正试图实现的目标。@Lionel Chan您是否尝试过groupBy('id')
@Gayan我没有问这个问题:)我的错@莱昂内尔:对不起@博蒂尔。您是否尝试groupBy('id')
。看看我的答案,看看是否符合你的需要。