How do you alias an eloquent model? For example if I have an SQL query as follows:
SELECT one.name
, one.id
, one.name AS sortkey1
, CAST(NULL AS UNSIGNED) AS sortkey2
, CAST(NULL AS UNSIGNED) AS sortkey3
FROM locations AS one
WHERE one.parent_id = 0
UNION ALL
....
In my repository I would have something as follows:
$first = $this->model->where('one.parent_id', '=', 0)
->select('one.name'
, 'one.id'
, 'one.name AS sortkey1'
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey2')
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey3'));
So how can you alias the model. In the above example the model maps the the locations table and in my eloquent query I want to alias it as one
instead of locations
You can use from
this way:
$first = $this->model->from('locations as one')
->where('one.parent_id', '=', 0)
->select('one.name'
, 'one.id'
, 'one.name AS sortkey1'
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey2')
, DB::raw('CAST(NULL AS UNISIGNED) AS sortkey3'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With