2016-08-22 90 views
0

我正在使用Laravel 5,我有一張用戶表,以及兩個包含用戶之間關係的「客戶」和「僱員」表。從原始查詢中獲取Laravel雄辯集合

我想獲得登錄用戶的所有客戶和員工。

我有這對於工作正常原始查詢:

select users.* from clients, users 
where clients.id_marchand = 8 and users.id = clients.id_client 
union 
select users.* from employes, users 
where employes.id_marchand = 8 and users.id = employes.id_employe 
order by `seen` asc, `created_at` desc limit 25 offset 0 

原始查詢返回一個數組,但我需要一個雄辯的集合,如:

return $this->model 
->where(...) 
->oldest('seen') 
->latest() 
->paginate($n); 

我試着很多不同的可能性,但他們都沒有工作...

是不是有辦法做到這一點與子查詢,或其他?

+0

我已經特里('clients') - > where('clients.id_marchand','=',auth() - > user() - 這是一個類似於這樣的事情: '$ liste_clients = $ this-> model - > > id) - > where('users.id','=','clients.id_client'); $ liste_employes = $ this-> model - > with('employes') - > where('employes.id_marchand','=',auth() - > user() - > id) - > where ('users.id','=','employes.id_employe') - > union($ liste_clients); 返回$ liste_employes - >最古老( '看到') - >最新的() - > PAGINATE($ N);' 但我最大的問題是,我無法從查詢生成器幾個表選擇。 –

回答

0

你可以直接使用原始查詢是這樣的:

return DB::select(DB::raw(" 
    select * from table_name where column_name = :columnName 
"),['columnName'=>$columnName]); 
0

我已經試過這樣:

return DB::select(DB::raw('select users.* from clients, users where clients.id_marchand = 8 and users.id = clients.id_client union select users.* from employes, users where employes.id_marchand = 8 and users.id = employes.id_employe order by `seen` asc, `created_at` desc limit 25 offset 0'), [1]); 

但在UserController.php,我有:

public function indexSort($role) 
{ 
    $counts = $this->user_gestion->counts(); 
    $users = $this->user_gestion->index(25, $role); 
    $links = $users->render(); 
    $roles = $this->role_gestion->all(); 

    return view('back.users.index', compact('users', 'links', 'counts', 'roles'));  
} 

Laravel告訴我,我無法將「渲染」方法應用於陣列...

所以我覺得我需要一個雄辯的集合,而不是...

我已經嘗試了一些這樣的事情太:

$liste_clients = $this->model 
->with('clients') 
->where('clients.id_marchand', '=', auth()->user()->id) 
->where('users.id', '=', 'clients.id_client'); 

$liste_employes = $this->model 
->with('employes') 
->where('employes.id_marchand', '=', auth()->user()->id) 
->where('users.id', '=', 'employes.id_employe') 
->union($liste_clients); 

return $liste_employes 
->oldest('seen') 
->latest() 
->paginate($n); 

,但我最大的問題是,我無法從中選擇查詢生成器的幾個表。

即使加入:

$liste_clients = $this->model 
->join('clients', function ($join) { 
    $join->on('users.id', '=', 'clients.id_client') 
    ->where('clients.id_marchand', '=', auth()->user()->id); 
}); 

$liste_employes = $this->model 
->join('employes', function ($join) { 
    $join->on('users.id', '=', 'employes.id_employe') 
    ->where('employes.id_marchand', '=', auth()->user()->id); 
}) 
->union($liste_clients); 

return $liste_employes 
->oldest('seen') 
->latest() 
->paginate($n); 

我得到一個錯誤信息:

SQLSTATE [21000]:基數違規:1222條使用的SELECT語句具有不同的列數(SQL:(從users內上users加入employes SELECT COUNT(*)作爲骨料。id = employesid_employeemployesid_marchand = 8)聯合(SELECT * FROM內部加入clientsusersid = clientsid_clientclientsid_marchand = 8)seen遞增,created_at DESC)

上的其他論壇秩序,一個人告訴我簡單地做這在Repository:

return $this->model 
->with('role', 'clients', 'employes') 
->oldest('seen') 
->latest() 
->paginate($n); 

,然後用一些的foreach在玩的觀點,如:

@foreach($users->clients as $client) 

但我無法得到任何結果,我不知道爲什麼...