我有一個名爲records
的表,其中user_id
列鏈接到users
表以設置所有權。Laravel帶有多個表的搜索DB
我可以正確地過濾由title
記錄與搜索字符串:
$records->where('title', 'LIKE', '%'.$search.'%');
但我想也返回一個包含users.firstname
和users.lastname
的結果,這是我的(可怕)加入嘗試:
$records->join('users', 'users.id', '=', 'records.user_id')
->where('users.firstname', 'LIKE', '%'.$search.'%')
->orWhere('users.lastname', 'LIKE', '%'.$search.'%')
->orWhere('title', 'LIKE', '%'.$search.'%');
// SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous
謝謝,但我去掉了不必要的代碼,以保持它乾淨。我會更新示例以避免混淆。 – gyo