我已經試過這樣:
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
= employes
。id_employe
和employes
。id_marchand
= 8)聯合(SELECT * FROM內部加入clients
users
。 id
= clients
。 id_client
和clients
。id_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)
但我無法得到任何結果,我不知道爲什麼...
我已經特里('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);' 但我最大的問題是,我無法從查詢生成器幾個表選擇。 –