[SOLVED] How to call accessor in javascript blade laravel?


This Content is from Stack Overflow. Question asked by Ali Yazdanifar

I have set an accessor in the model

public function getDayInfoAttribute()
    $fromDate = $this->program->from_date;
    $toDate = $this->program->to_date;
    $period = CarbonPeriod::create($fromDate, $toDate);
    $days = [];
    foreach ($period as $p) {
        $day = verta($p)->formatWord('l');
        $dayOfWeek = verta($p)->dayOfWeek;
            'id' => $dayOfWeek,
            'day' => $day,
            'date' => $p->format('Y-m-d')
    return $days;

My javascript codes are written in blade.

Now I want to call it in javascript code.

     let fields =@json($prgFields);

Please help if you have any idea to solve this problem. Thanks


From the documentation:

If you would like these computed values to be added to the array / JSON representations of your model, you will need to append them.

class PrgField
    protected $appends = [ 'dayInfo' ];

This Question was asked in StackOverflow by Ali Yazdanifar and Answered by shaedrich It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?