The error comes from how you're using the concat function in your Eloquent query. In Laravel's query builder, you can't use PHP's concat() function directly in the select clause. Instead, you need to use a raw expression with DB::raw() and proper SQL syntax.
Here's how you can fix your query:
use Illuminate\Support\Facades\DB;
Member::query()
->join('students', 'students.id', '=', 'ensemble_members.student_id')
->join('users', 'users.id', '=', 'students.user_id')
->where('ensemble_id', $this->form->ensembleId)
->where('school_year', $this->program->school_year)
->select(
'ensemble_members.*',
'users.name',
'users.last_name',
'users.first_name',
DB::raw("CONCAT(users.name, ' (', students.id, ')') AS studentData")
)
->orderBy('users.last_name')
->orderBy('users.first_name')
->get()
->toArray();
Key Points:
- Use
DB::raw()to write raw SQL expressions. - The SQL
CONCAT()function is used inside the raw expression. - The alias (
AS studentData) should be inside the string passed toDB::raw().
This should resolve the syntax error and return the desired concatenated value as studentData.