Laravel Eloquent 查询关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19047315/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:29:13  来源:igfitidea点击:

Laravel Eloquent querying relationships

phpmysqllaraveleloquent

提问by Andrew Willis

I have created a messaging system for a project I am working on and appear to have hit a bit of a stumbling block.

我为我正在从事的项目创建了一个消息传递系统,但似乎遇到了一些绊脚石。

I have the following table structures (irrelevent columns omitted for clarity)

我有以下表格结构(为清楚起见省略了无关的列)

messages
-------------------------
from_id    to_id    spam

users
-------------------------
id    group_id

groups
-------------------------
id    access_level

the access level uses bitwise permissions

访问级别使用按位权限

001 read
010 write
100 delete

so to read and write, the access level would be '3'

所以要读取和写入,访问级别将是“3”

users can have permissions revoked by site admin, so what I am trying to do is to select messages which have been sent to users where the sender still has write permissions.

用户可以拥有站点管理员撤销的权限,所以我想要做的是选择已发送给发件人仍然具有写入权限的用户的消息。

my models are set up as such (again only relevant parts shown)

我的模型是这样设置的(同样只显示相关部分)

class User extends Eloquent {
    public function group() {
        return $this->belongsTo('Group');
    }
}

class Message extends Eloquent {
    public function to() {
        return $this->belongsTo('User', 'to_id');
    }
    public function from() {
        return $this->belongsTo('User', 'from_id');
    }
}

I have tried the following

我已经尝试了以下

Message::with(['from' => function($query)
{
    $query->with(['group' => function($_query)
    {
        $_query->where('access_level', '&', 2);
    }]);
}])
->where('to_id', Auth::user()->id)
->get();

This gets the messages sent to the currently logged in user where the senders group's access level still enables the user to write messages (theoretically) however it is simply returning all the messages sent to the user.

这会将消息发送给当前登录的用户,其中发件人组的访问级别仍然允许用户编写消息(理论上),但它只是返回发送给用户的所有消息。

I have also tried specifically targeting the group (groups 6 and 7 are banned and unconfirmed accounts):

我也尝试过专门针对该组(第 6 组和第 7 组被禁止和未经确认的帐户):

Message::with(['from' => function($query)
{
    $query->whereNotIn('group_id', [6,7])
}])
->where('to_id', Auth::user()->id)
->get();

which also returns all of the messages to the user.

它还将所有消息返回给用户。

tl;dr

tl;dr

How, using Eloquent, would I select all messages sent to a user where the senders group has write permissions?

如何使用 Eloquent 选择发送给发件人组具有写入权限的用户的所有消息?

edit

edit

Of course, ::withwill not affect the messages which get pulled, as it only does lazy loading of joined tables.

当然,::with不会影响被拉取的消息,因为它只会延迟加载连接表。

回答by Andrew Willis

My solution was to do this:

我的解决方案是这样做:

Message::select(DB::raw("messages.*"))
    ->join("users AS from", "from.id", "=", "from_id")
    ->join("groups", "groups.id", "=", "from.group_id")
    ->whereRaw("groups.access_level & 2 = 2")
    ->where('to_id', '=', Auth::user()->id);

Which does exactly what I wanted it to.

这正是我想要的。

As stated in my question ::withonly affects the loading of the data.

正如我的问题中所述,::with仅影响数据的加载。

回答by Glad To Help

How about this?

这个怎么样?

 Message::with(['from' => function($query){
              $query->where('group_id', '=', '2');
    }])
    ->where('to_id', Auth::user()->id)
    ->get();

Have you tried filtering the "from" for a certain group_id, like 2? I think a query like this should filter your results correctly.

您是否尝试过过滤某个 group_id 的“from”,例如 2?我认为这样的查询应该正确过滤您的结果。