R
R
Ruslan Absalyamov2018-04-01 20:33:55
Laravel
Ruslan Absalyamov, 2018-04-01 20:33:55

How can static data be redefined from the database?

There is certain data that numbers come in, but I need to make sure that these numbers change immediately to a value. I tried to look at the mutators, but I didn’t catch up, it’s possible to somehow apply them to the data.
Here's how

$shedules = Shedule::where('user_id', $user)->where('date', strtotime(date('Y-m-d')))->get();
        foreach ($shedules as $shedule){
            $shedule->type = $shedule->type == 1 ? 'Групповое занятие' : 'Индивидульные занятие'; // Вот эти моменты часто повторяются
        }

Only you can put it somehow into the model and return one of the values ​​and pass it back, thereby redefining the type. Type comes 1 or 2, but I need them to change 1 => Group lesson or 2 => Individual lesson.
Just haven’t caught up yet how to implement with override

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2018-04-01
@rusline18

If you read the documentation carefully before you start writing, and judging by your question, you seem to have read, but did not bother to understand.
You can add a method to your model, either under the same name type, or make it more readable to name it type_name, in order to do this you just need to add a reader

public function getTypeNameAttribute()
  {
    return $this->type == 1 ? 'Групповое занятие' : 'Индивидулаьные занятие';
  }

After that you will have available $schedule->type_name attribute in your model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question