'laravel accessors not working with select() statement

Laravel accessors not working when there is select statement in query.

App\Models\Doctor::select('name')->first()->doctor_name

I also have added accessors name in appends

protected $appends = ['doctor_name'];

and tried by using full function name

App\Models\Doctor::first()->getDoctorNameAttribute()

This only works when I don't use select() in query

enter image description here

what is the solution ?



Solution 1:[1]

You must select the field where you established the accessor, doctor_name in this case

App\Models\Doctor::select(['name', 'doctor_name'])->first()->doctor_name

It works when you don't use select() because it select all fields.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mariano ArgaƱaraz