Laravel 的 Eloquent ORM 中的 join()->where()

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

join()->where() in Laravel's Eloquent ORM

phpmysqllaravelormeloquent

提问by user1960364

I'm essentially building a multi-site site under a single domain using subdomains for each account. Each account has a unique set of usernames, but not unique across all sites. Therefore, there could be a 'tom' on account 1 and a 'tom' on account 2, but not 2 tom's on any single account.

我基本上是在单个域下使用每个帐户的子域构建一个多站点站点。每个帐户都有一组唯一的用户名,但并非在所有站点中都是唯一的。因此,帐户 1 上可能有一个“tom”,帐户 2 上可能有一个“tom”,但任何单个帐户上都没有 2 个。

My users table has a column specifying the account_id and my controller has the subdomain. So, when querying a user, how can I ensure that the users account_idmatches an accounts idwhere the accounts id = users.account_idand accounts.subdomain = $subdomain?

我的用户表有一个指定 account_id 的列,我的控制器有子域。那么,在查询用户时,我如何确保用户account_id匹配id帐户id = users.account_id和帐户所在的帐户accounts.subdomain = $subdomain

Here is what I'm trying:

这是我正在尝试的:

$user = User::whereUsername($username)
    ->leftJoin('accounts', 'accounts.id', '=', 'users.account_id')
    ->where('accounts.id', '=', 'users.account_id')
    ->where('accounts.subdomain', '=', $subdomain)
    ->firstOrFail();

The problem with this is that it's taking users.account_idas a literal string and not a table reference.

这样做的问题是它是users.account_id作为文字字符串而不是表引用。

Secondly, in relation to this setup, is there a better way to approach this so that anytime I'm on a subdomain it will pull only relevant data without me have to be repetitive?

其次,关于这个设置,有没有更好的方法来解决这个问题,这样我在子域上的任何时候它都会只提取相关数据而我不必重复?

Update:I managed to get the query working by using DB::raw() but am curious if there is a better way.

更新:我设法使用 DB::raw() 使查询工作,但很好奇是否有更好的方法。

$user = User::whereUsername($username)
    ->leftJoin('accounts', 'accounts.id', '=', 'users.account_id')
    ->where('accounts.id', '=', DB::raw('users.account_id'))
    ->where('accounts.subdomain', '=', $subdomain)
    ->firstOrFail();

Also, still interested in receiving an answer to my second question.

此外,仍然有兴趣收到我的第二个问题的答案。

回答by rafwell

I managed to get the query working by using DB::raw() but am curious if there is a better way.

我设法使用 DB::raw() 使查询工作,但很好奇是否有更好的方法。

You have the method whereRaw.

你有方法 whereRaw。

$user = User::whereUsername($username)
->leftJoin('accounts', 'accounts.id', '=', 'users.account_id')
->whereRaw('accounts.id = users.account_id')
->where('accounts.subdomain', '=', $subdomain)
->firstOrFail();

Secondly, in relation to this setup, is there a better way to approach this so that anytime I'm on a subdomain it will pull only relevant data without me have to be repetitive?

其次,关于这个设置,有没有更好的方法来解决这个问题,这样我在子域上的任何时候它都会只提取相关数据而我不必重复?

You can use globalScopes: https://laravel.com/docs/5.3/eloquent#query-scopes

您可以使用 globalScopes:https://laravel.com/docs/5.3/eloquent#query-scopes

回答by fisharebest

->where(X, Y)compares the column X with the valueY.

->where(X, Y)将列 X 与Y进行比较。

->whereColumn(X, Y)compares the column X with the columnY.

->whereColumn(X, Y)列X与比较Y.