我正在尝试从后端获取信息到vuejs multiselect组件。但是我很难将原始sql转换为laravel。
这是我需要转换的原始代码:
select name, users.id
from users, ( select if(user_id = $bruker, friend_id, user_id ) as venn, friends.*
from friends
where user_id = $bruker or friend_id = $bruker) as venner
where users.id = venner.venn;
我尝试了此代码,但是在组件中显示为“列表为空”。 当我更改whereRaw时,我可以获得一些信息,但没有正确的信息。
$users = \DB::table('users',
DB::raw(
"select if(user_id = $bruker, friend_id, user_id ) as venn,
friends.*
from friends
where user_id = $bruker or friend_id = $bruker) as venner
"))
->whereRaw("users.id = venner.venn")
->pluck('name');
对不起,英语不好。
答案 0 :(得分:0)
我解决了我的问题。我需要做的就是使数组从查询到集合。这样我冷使用原始SQL。 maby不是最好的解决方案,但它确实有效。
代码如下:
$bruker = \Auth::user()->id;
$coll = \DB::select
("SELECT name
from users, ( select if(user_id = $bruker, friend_id, user_id ) as venn, friends.*
from friends
where user_id = $bruker or friend_id = $bruker) as venner
where users.id = venner.venn
and accepted = 1
");
$users = collect($coll)->pluck('name');
return $users;