V
V
Vit2016-07-21 20:39:26
Laravel
Vit, 2016-07-21 20:39:26

How to do validation in the model?

It is necessary to make data validation with the least amount of code at $model->create($request->all()); Like Esensi Model Traits, but I want to do without third-party packages.
Is it possible to?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-07-22
@wielski

Create a main model, say BaseModel.php
In it, override the Create method

public static function create(array $attributes = [])
    {
        // тут проводите свою валидацию, и возвращайте false если не прошли
        $model = new static($attributes);
        $model->save();
        return $model;
    }

Well, extend all your models with this model instead of Illuminate\Database\Eloquent\Model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question