2016-08-22 54 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) 

但我无法得到任何结果,我不知道为什么...