php - Laravel - Querybuilder with join and concat -
Trying to drag all the users from the users table, which matches a certain group in users_groups pivot table. I am using Centri 2 from Cartelist BTID.
This works together with all users first and last names.
User :: Select ('full_name' as' DB :: raw ('CONCAT (last_name,', 'first_name'), 'id') - & gt; Where ('active', '=', '1') - & gt; Order By ('last_name') - & gt; Lists ('full_name', 'id'); When I try to change it to filter users who are not related to any specific group, I get a syntax error.
User :: Select (DB :: Raw ('SELECT CONCAT (user.last_name,', 'user.first_name'), 'user.full_name'), 'user.id' , 'Users_groups.group_id', 'users_groups.user_id') - & gt; Join ('Users_groups', 'user.id', '=', 'users_groups.user_id') - & gt; Where ('user.activated', '=', '1') - & gt; Where ('users_groups.group_id', '=', $ group) - & gt; Orderbuy ('user.last_name') - & gt; Lists ('user.full_name', 'user.id'); Any kind of leap in the right direction will be highly appreciated.
Edit: Syntax Error
SQLSTATE [42000]: Syntax Error or Access Violation: 1064 You have an error in your SQL syntax; Manually check to use 'SELECT CONCAT (user.last_name', '', user.first_name) near the correct syntax for the MySQL server version, user.full_name, 'user_.`' on line 1 (SQL: Select CONCAT (user.last_name, ",", user.first_name) as user.full_name, `user` .``,` users_groups`.`group_id`, `users_groups` .` Users_groups` on` user` On `` users_groups```user_id`` where `user` .` deleted_at` is zero and` user`. 'Active' = 1 and `users_groups``` group_id` = 9 command` user` .` ____ name` By ASC) "post-text" itemprop = "te Xt "> Logan's Answer I Found The Right Direction I had to remove all 'users' too. The prefix because this user was calling the model, already I think. This query works:
User :: Select (DB :: Raw ('full_name' as CONCAT (last_name, ',' first_name), 'id') - & Gt; ('Users_groups', 'id', '=', 'users_groups.user_id') - & gt; Where ('active', '=', '1') - & gt; Where ('users_groups.group_id', '=', $ group) - & gt; Orderbuy ('last_name') - & gt; Lists ('fullname', 'id'); Thanks everyone! Hopefully if someone else walks on it then they will get guidance with this question.
Comments
Post a Comment