多个连接laravel php

时间:2017-01-09 00:15:27

标签: php laravel laravel-5

我有一个博客应用,它有三个表,我想查看任何搜索查询。

三个表:public void findRandomObject() { BasicDBObject object = new BasicDBObject("$sample", new BasicDBObject("size", 1)); AggregationOutput output = getCollection().aggregate(object); for (DBObject result : output.results()) { LOGGER.info("output >> " + result); } } postscategories

型号:

发表:

tags

代码:

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 它应匹配doughnutspost titlecategory name,如果任何与之相关的匹配都应该出现在搜索结果中。

我尝试了什么

tag name

但这似乎给了我重复的结果并且效果不佳。

我想知道更好的解决方案吗?

1 个答案:

答案 0 :(得分:1)

当我发现自己在查询中使用多个连接时(特别是在SELECT查询中)我通常会实现一个数据库view来封装查询背后的逻辑。

像这样创建迁移

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';
}

然后您可以简化查询,如

$post = PostView::where(function ($query) use ($search) {
            return $query->orWhere('title', 'LIKE', $search)
                ->orWhere('tags_name', 'LIKE', $search)
                ->orWhere('categories_name', 'LIKE', $search);
        })
            ->groupBy('id')
            ->paginate(5);

重要的是要记住,这完全取决于具体情况。 对我来说,这是一个更清洁的方法,而不是让我的查询变得混乱。我想你也可以采用这种方法。感谢。

注意:您可能需要进一步修改viewquery以表示您要实现的目标。