您好,我正在尝试添加一个搜索,该搜索将基于多个列从两个不同的表中搜索数据。我已经成功地从一个表中获取了结果,但是当我尝试将搜索与其他表一起加入时,它不返回任何记录。这是我的代码。
控制器
public function getCustomers() {
if (request()->ajax()) {
$term = request()->input('q', '');
$business_id = request()->session()->get('user.business_id');
$user_id = request()->session()->get('user.id');
$contacts = Contact::where('business_id', $business_id);
$selected_contacts = User::isSelectedContacts($user_id);
if ($selected_contacts) {
$contacts->join('user_contact_access AS uca', 'contacts.id', 'uca.contact_id')
->where('uca.user_id', $user_id);
}
if (!empty($term)) {
$contacts->where(function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%')
->orWhere('supplier_business_name', 'like', '%' . $term . '%')
->orWhere('mobile', 'like', '%' . $term . '%')
->orWhere('cars.registration_number', 'like', '%' . $term . '%')
->orWhere('contacts.contact_id', 'like', '%' . $term . '%');
})->join('cars', 'contacts.id', 'cars.contact_id')
->where('cars.contact_id', 'contacts.id');
}
$contacts = $contacts->select(
'contacts.id', DB::raw("IF(contacts.contact_id IS NULL OR contacts.contact_id='', name, CONCAT(name, ' (', contacts.contact_id, ')')) AS text"), 'mobile', 'landmark', 'city', 'state', 'pay_term_number', 'pay_term_type'
)
// ->onlyCustomers()
->get();
if (count($contacts) <= 0) {
$contacts = Car::where('contact_id', 'like', '%' . $term . '%')->get();
}
return json_encode($contacts);
}
}
我在这里做错了什么。预先谢谢你