[英]Additional where in laravel query builder
I have joined table in my query but i need to validate the value of column in that table, it returns 我在查询中加入了表格,但是我需要验证该表格中列的值,它返回
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'on' in 'on clause'
$interests = DB::table('projects')
->where('projects.published', 'y')
->whereDate('projects.created_at', Carbon::now()->subDay())
->join('project_tag', 'project_tag.project_id', '=', 'projects.id') //get project tags
->join('mailings', 'mailings.interests', '=', 'on') //get mailing lists with on value in interests column
->join('users', 'users.id', '=', 'mailings.user_id') //get mailing users
->join('user_interests', 'user_interests.user_id', '=', 'users.id') //get mailing users intrests (same as tags in projects)
->select('projects.*', 'users.*')
->get();
my issue is in this line 我的问题在这一行
->join('mailings', 'mailings.interests', '=', 'on')
I just want to join mailings
where their intrests
column value is set to on
我只想加入其intrests
列值设置为on
mailings
Any idea? 任何想法?
I changed my issued line to: 我将发布行更改为:
->join('mailings', function ($join) {
$join->where('mailings.interests', '=', 'on');
})
now it's returning results but output data are duplicated and not complete. 现在它正在返回结果,但是输出数据重复且不完整。 here is what i get on dd
这是我在dd
得到的
lluminate\Support\Collection {#819
#items: array:6 [
0 => {#824
+"title": "test 1"
+"slug": "test-1"
+"budget": 23454235.0
+"name": "Diana Bosco"
+"email": "arunolfsson@example.net
}
1 => {#827
+"title": "test 1"
+"slug": "test-1"
+"budget": 23454235.0
+"name": "Diana Bosco"
+"email": "arunolfsson@example.net
}
2 => {#820
+"title": "test 1"
+"slug": "test-1"
+"budget": 23454235.0
+"name": "admin"
+"email": "admin@admin.com"
}
3 => {#826
+"title": "test 1"
+"slug": "test-1"
+"budget": 23454235.0
+"name": "admin"
+"email": "admin@admin.com"
}
4 => {#823
+"title": "test 1"
+"slug": "test-1"
+"budget": 23454235.0
+"name": "Aurelia Treutel I"
+"email": "korey13@example.net"
}
5 => {#825
+"title": "test 1"
+"slug": "test-1"
+"budget": 23454235.0
+"name": "Aurelia Treutel I"
+"email": "korey13@example.net"
}
]
To be clear Logic
要明确 Logic
Use ->join()
to join the mailings
table via FK reference, same as you have done for the other tables, then change the where
clause to an array of conditions: 使用->join()
通过FK引用加入mailings
表,就像您对其他表所做的一样,然后将where
子句更改为条件数组:
->where([
['projects.published', '=', 'y'],
['mailings.interests', '=', 'on'],
])
Apply the filter as, 将过滤器应用为
$interests = DB::table('projects')
->where('projects.published', 'y')
->whereDate('projects.created_at', Carbon::now()->subDay())
->join('project_tag', 'project_tag.project_id', '=', 'projects.id') //get project tags
// the filter
->where('mailings.interests', 'on')
->join('users', 'users.id', '=', 'mailings.user_id') //get mailing users
->join('user_interests', 'user_interests.user_id', '=', 'users.id') //get mailing users intrests (same as tags in projects)
->select('projects.*', 'users.*')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.