[英]Laravel Eloquent Search Multiple Related Tables
I am struggling with figuring out how to run a like
query against multiple related tables. 我正在努力弄清楚如何对多个相关表运行
like
查询。
I have a submissions
table that has related users
, mcd_forms
, and submission_statuses
tables. 我有一个
submissions
有相关表users
, mcd_forms
和submission_statuses
表。
Here is my code for running a LIKE
statement with the given $terms_like
. 这是我的代码,用于使用给定的
$terms_like
运行LIKE
语句。
$submission = new Submission;
$terms_like = '%'.$search_terms.'%';
$data['submissions'] = $submission
->join('users as users', 'users.id', '=', 'submissions.user_id')
->join('mcd_forms as forms', 'forms.submission_id', '=', 'submissions.id')
->join('submission_statuses as statuses', 'statuses.id', '=', 'submissions.submission_status_id')
->where(function($q) use ($terms_like) {
$q->where('users.user_group_id', '=', Auth::user()->user_group_id)
->orWhere('forms.name', 'like', $terms_like)
->orWhere('forms.custom_id', 'like', $terms_like)
->orWhere('forms.start_date', 'like', $terms_like)
->orWhere('forms.end_date', 'like', $terms_like)
->orWhere('forms.soft_sell_date', 'like', $terms_like)
->orWhere('forms.region', 'like', $terms_like)
->orWhere('statuses.status_formatted', 'like', $terms_like);
});
No matter what I try it returns incorrect results. 不管我尝试什么,它都会返回错误的结果。 What am I doing wrong?
我究竟做错了什么?
In your query above, since you are not using the "%" symbol, your like clause is working as an "=" since it's trying to match the whole word. 在上面的查询中,由于您未使用“%”符号,因此您的like子句将尝试匹配整个单词,因此其作用为“ =”。
Replace all the "where" clause like this: 像这样替换所有“ where”子句:
->orWhere('forms.name', 'like', "%".$terms_like."%")
This will try to match the word anywhere in the text. 这将尝试匹配文本中任何位置的单词。
You can try 'like' operator following this: 您可以按照以下方法尝试“赞”运算符:
$users = DB::table('users')
->where('forms.name','LIKE', '%'.$terms_like.'%')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.