在Laravel,如何建立一个“喜欢”的关系,表?另外,如何在查询中包含该信息? [英] In Laravel, how to set up a relationship for a "likes" table? Also, how to include that information in a query?

查看:134
本文介绍了在Laravel,如何建立一个“喜欢”的关系,表?另外,如何在查询中包含该信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表格:用户想法 ideas_likes 。 ideas_likes表格如下所示:

I have three tables: users, ideas, and ideas_likes. The ideas_likes table looks like:

ideas_likes
------------
id        primary key
user_id   foreign key
idea_id   foreign key
liked     boolean

用户和想法之间已经建立了一对多关系。它看起来像这样:

There's already a one-to-many relationship set up between users and ideas. It looks something like this:

class User extends Ardent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $table = 'users';

    public function ideas()
    {
        return $this->hasMany('Idea');
    }   

}

同样,想法模型看起来像这个:

Similarly, the idea model looks like this:

class Idea extends Ardent {

    protected $table = 'ideas';

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

}

我的第一个问题是:如何我创建了IdeaLike模型?一旦完成,使用Eloquent,如何检索所有喜欢的想法为用户?

My first question is: How do I create the IdeaLike model? And once that is finished, using Eloquent, how do I retrieve all liked ideas for a user?

============= ================================================== ==========

===========================================================================

Lucasgeiter的更新解决方案运行良好。

Lucasgeiter's updated solution worked great.

推荐答案

首先,不需要创建一个 IdeaLike 模型。您只需要多对多关系

First, there's no need to create an IdeaLike model. You just need a many-to-many relationship

public function ideas(){
    return $this->belongsToMany('Idea', 'ideas_likes')->withPivot('liked');
}



想法



Idea

public function users(){
    return $this->belongsToMany('User', 'ideas_likes')->withPivot('liked');
}

(通过添加 withPivot 我告诉Laravel,我想从结果中包含的数据透视表中选择喜欢列)

(By adding withPivot I tell Laravel that I want to have the liked column from the pivot table included in the result)

$user = User::find(1);
$likedIdeas = $user->ideas()->where('liked', true)->get();



顺便说一句:你不需要指定 $ table 如果它是模型名称的复数。


By the way: You don't need to specify the $table if it is the plural of the model name.

看起来你实际上真的需要两者,一对多和多对多的关系。

It looks like you actually really do need both, a one-to-many and a many-to-many relation.

所以你最后的关系会看起来像这样:

So your final relations would look like this:

public function ideas(){
    return $this->hasMany('Idea');
}

public function liked(){
    return $this->belongsToMany('Idea', 'ideas_likes')->withPivot('liked');
}



想法



Idea

public function likes(){
    return $this->belongsToMany('User', 'ideas_likes')->withPivot('liked');
}

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

(我刚刚选择了对我有意义的关系的名字。你可以改变它们)

(I just chose names for the relations that made kind of sense to me. You can obviously change them)

喜欢某个用户的想法:( id = 1

Liked ideas by a certain user: (id = 1)

$ideas = Idea::where('user_id', 1)->whereHas('likes', function($q){
    $q->where('liked', true);
})->get();

这篇关于在Laravel,如何建立一个“喜欢”的关系,表?另外,如何在查询中包含该信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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