0

I have a collection of user skills but i need is to get only skills as an array i tried this

$builder->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id', function ($join){
    $join->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
})
->groupBy('job_applicants.job_id')
->get();

The exception message : Object of class Closure could not be converted to string

0

1 Answer 1

2

The join closure should be the first and only argument after the table name.

->join('user_skills', function ($join) {
    $join->on('users.id', '=', 'user_skills.user_id')
         ->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
});

However, a selectRaw inside the join closure doesn't make a whole lot of sense. Logically, this isn't where you select data. You've joined the table, your select statement belongs in the main query, it doesn't belong contained within the join, like so:

$builder->selectRaw("job_applicants.*, GROUP_CONCAT(user_skills.skill, ', ')")
    ->where('job_id', $value)
    ->join('users', 'job_applicants.user_id', '=', 'users.id')
    ->join('user_skills', 'users.id', '=', 'user_skills.user_id')
    ->groupBy('job_applicants.job_id')
    ->get();
Sign up to request clarification or add additional context in comments.

1 Comment

$builder->selectRaw("job_applicants.*, GROUP_CONCAT(user_skills.skill, ', ') as Skills") ->where('job_id', $value) ->join('users', 'job_applicants.user_id', '=', 'users.id') ->join('user_skills', 'users.id', '=', 'user_skills.user_id') ->groupBy('job_applicants.job_id');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.