G
G
Glory2021-05-13 21:59:00
Laravel
Glory, 2021-05-13 21:59:00

How to add a constant field in Eloquent in a query?

The task is simple, but I can not find in the docks how to implement it. You

need to make a selection of fields from the model, but add one more field to each record in the form of a constant, for example, "model type" Cd" or at least how to formulate this request?
$cd = Cd::select('cds.slug', 'cds.name')->get();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin B., 2021-05-13
@Kostik_1993

Probably setAttribute and the $appends property in the model will help you to do

A
Anton Anton, 2021-05-13
@Fragster

https://laravel.com/docs/5.8/queries#raw-expressions

V
Vladimir Kudrya, 2021-05-14
@Mugenzo

Sounds like it needs

K
Kladislav, 2021-05-18
@Kladislav

You need a global scope which will have $query->addSelect([....])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question