2014-11-21 72 views
4

我如何準備在Laravel下面的MySQL查詢?laravel mysql查詢與多個地方或內部聯接

這是我沒有成功使用的代碼:

$user_list = DB::table('users') 
->where('users.user_id' ,'=', $clientId) 
->where('users.firstname', 'LIKE', '%'.$_POST['name'].'%') 
->orWhere('users.lastname', 'LIKE', '%'.$_POST['name'].'%') 
->orWhere('users.email', 'LIKE', '%'.$_POST['name'].'%') 
->join('users_roles', 'users.id', '=', 'users_roles.user_id')       
->where('users_roles.role_id', '=', Role::USER_PARTICIPANT) 
->get(); 

的條件必須是:

  1. users.user_id == $clientId
  2. users.firstname == '%'.$_POST['name'].'%' OR users.lastname == '%'.$_POST['name'].'%' OR users.email == '%'.$_POST['name'].'%'
  3. 內users_roles和用戶通過users.id == users_roles.user_id when users_roles.role_id == Role::USER_PARTICIPANT
  4. 渡之間加入
+0

我試過了。我用我的代碼更新了這個問題 – Alberto 2014-11-21 14:26:55

回答

14

好了,它看起來像你的問題是與嵌套其中

試試這個:

$name = $_POST['name']; // I'd recommend you use Input::get('name') instead... 

$user_list = DB::table('users') 
    ->where('users.user_id' ,'=', $clientId) 
    ->where(function($query) use ($name){ 
     $query->where('users.firstname', 'LIKE', '%'.$name.'%'); 
     $query->orWhere('users.lastname', 'LIKE', '%'.$name.'%'); 
     $query->orWhere('users.email', 'LIKE', '%'.$name.'%'); 
    }) 
    ->join('users_roles', 'users.id', '=', 'users_roles.user_id') 
    ->where('users_roles.role_id', '=', Role::USER_PARTICIPANT) 
    ->get(); 
+0

我得到了錯誤:語法錯誤,意外的'使用'(T_STRING),期待'{'。在這裏:函數($查詢)使用($名稱){ – Alberto 2014-11-21 14:39:35

+0

Ooops錯字,它實際上'使用' – lukasgeiter 2014-11-21 14:48:35

+0

謝謝!這工作。順便說一句,在查詢後你會錯過分號。 – Alberto 2014-11-21 15:01:08

0

我覺得你要考慮使用的連接語句內聯函數的可能性。 ..你的where子句可能會碰撞 - 而且你真的想讓role_id子句加入你的連接。

http://laravel.com/docs/4.2/queries#joins

$clientId = 1; 
$name = 'Phillip'; 
$escaped = '%' . $name . '%'; 

$userList = DB::table('users') 
    ->where('users.user_id', $clientId) 
    ->where('firstname', 'LIKE', $escaped) 
    ->orWhere('lastname', 'LIKE', $escaped) 
    ->orWhere('email', 'LIKE', $escaped) 
    ->join('users_roles', function($join) { 
     $join->on('users.user_id', '=', 'users_roles.user_id') 
      ->where('users_roles.role_id', '=', Role::USER_PARTICIPANT); 
    })->get(); 

var_dump($userList); 

上述方法產生這個查詢:

select * 
from `users` 
inner join `users_roles` on `users`.`user_id` = `users_roles`.`user_id` and `users_roles`.`role_id` = ? 
where `users`.`user_id` = ? 
and `firstname` LIKE ? 
or `lastname` LIKE ? 
or `email` LIKE ? 

這是給我一個行回來,考慮到以下數據庫:

mysql> desc users; 
+-----------+------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+-----------+------------------+------+-----+---------+----------------+ 
| user_id | int(11) unsigned | NO | PRI | NULL | auto_increment | 
| firstname | varchar(255)  | NO |  | NULL |    | 
| lastname | varchar(255)  | NO |  | NULL |    | 
| email  | varchar(255)  | NO |  | NULL |    | 
+-----------+------------------+------+-----+---------+----------------+ 
4 rows in set (0.02 sec) 

mysql> desc users_roles; 
+---------+------------------+------+-----+---------+-------+ 
| Field | Type    | Null | Key | Default | Extra | 
+---------+------------------+------+-----+---------+-------+ 
| user_id | int(11) unsigned | NO |  | NULL |  | 
| role_id | int(11) unsigned | NO |  | NULL |  | 
+---------+------------------+------+-----+---------+-------+ 
2 rows in set (0.03 sec) 

mysql> select * from users; 
+---------+-----------+------------+--------------------+ 
| user_id | firstname | lastname | email    | 
+---------+-----------+------------+--------------------+ 
|  1 | Phillip | Harrington | [email protected] | 
+---------+-----------+------------+--------------------+ 
1 row in set (0.00 sec) 

mysql> select * from users_roles; 
+---------+---------+ 
| user_id | role_id | 
+---------+---------+ 
|  1 |  1 | 
+---------+---------+ 
1 row in set (0.00 sec) 
+0

我有同樣的問題,我正好在哪裏就像你:哪裏,或哪裏,或哪裏,但爲什麼當我用dd($ sql)檢查它時出現的第二個運算符「和」而不是或? – 2015-11-02 22:32:11