K
K
koida_yuri2017-02-01 10:55:43
Laravel
koida_yuri, 2017-02-01 10:55:43

How to execute code after executing get or first functions in laravel?

Hello. Written a small project on laravel 5.2.
There is a task: when selecting from a database of a certain model, do a check and replace some values, rush through all the controllers and add this check as it is not beautiful. In general, you need to somehow override the first, firstOrFail, get, etc. methods. for this model, is it possible to do this without resorting to creating crutches in the framework?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Finsh, 2017-02-01
@koida_yuri

Replacing values ​​in the model is done through mutators.
But if it’s very hard to make crutches with bicycles, then make a separate model class that inherits from the Illuminate\Database\Eloquent\Model model and override whatever you want there, and inherit the necessary models from your main model. But then again, laravel has a tool for your task.

A
Alexey Ukolov, 2017-02-01
@alexey-m-ukolov

Judging by the description of the task, you need to override the newInstance () method for this model and already manipulate the attributes there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question