'Laravel. Json column with cast as array are returning as a string when using JOIN in query
Laravel 8.
I have two tables(models) 'employees
' and 'positions
'. In a model 'employees
' I have a casts:
protected $casts = [
'academic_post' => 'array'
];
academic_post
is stored as a JSON. When I`m getting positions, joining them with employees:
$employees = DB::table('positions')
->select('employees.*', 'positions.*')
->where('department_tag', '=', $this->department_tag)
->leftJoin('employees', 'positions.email', '=', 'employees.email')
->get();
Problem: as employees
table is used in leftJoin, $employees[0]->academic_post
gives a string, not being casted as an array.
I know I can make a foreach loop and use json_decode
for each row to make an array, but why array casting is not working in this kind of query?
If Im using for example: Employee::find(1);
- Im getting column academic_post
as array
, as expected.
I just want it to work stright from a query, not using any kind of loop.
Any suggestions?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|