I have query:
$query = Task::query()->select(['tasks.*', 'rootTask.is_public as rootPublic']);
$query->leftJoin('tasks as rootTask', function ($join) {
$join->on('tasks._lft', '>=', 'rootTask._lft')
->whereNull('rootTask.deleted_at')
->where('tasks._lft', '<=', 'rootTask._rgt')
->whereNull('rootTask.parent_id');
});
when I try to get $task->rootPublic it returns NULL But if I do $query->toSql(); and run that SQL it returns 1.
What can be an issue?
When you use select(['tasks.*', 'rootTask.is_public as rootPublic']), Laravel may not map the aliased column correctly.
To resolve this issue, you can use the DB::raw method to define the alias directly in the select statement. Here's an updated version of your query:
I hope it can help you! Thanks