I'm trying to clean up some code that I made.
This is the current code:
$message = Message::with('comments')
->join('users', 'messages.created_by', '=', 'users.id')
->join('team_user', 'messages.created_by', '=', 'team_user.user_id')
->join('teams', 'team_user.team_id', '=', 'teams.id')
->join('roles', 'team_user.role_id', '=', 'roles.id')
->select('messages.id', 'messages.message', DB::raw('CONCAT(users.first_name, " ", users.last_name) AS created_by_name'), DB::raw('CONCAT(roles.name, " ", teams.name) AS function'))
->findOrFail($id);
I tried to make it like this:
$message = Message::with('comments')
->join('users', 'messages.created_by', '=', 'users.id')
->withFunction()
->findOrFail($id);
So I made a scope called withFunction
that looks like this:
return $query->join('team_user', 'messages.created_by', '=', 'team_user.user_id')
->join('teams', 'team_user.team_id', '=', 'teams.id')
->join('roles', 'team_user.role_id', '=', 'roles.id')->select(DB::raw('CONCAT(roles.name, " ", teams.name) AS function'));
But because I use this scope where I select specific column, I cant use the select in my query as well. I want it to look like this:
$message = Message::with('comments')
->join('users', 'messages.created_by', '=', 'users.id')
->withFunction()
->select('messages.id', 'messages.message')
->findOrFail($id);
So I specify the columns returned from the scope and from the query itself. I know I can't have 2 select's in a query, but is there any way this would be possible?
Would be great if you could just return columns in the scope to use it through the whole application.