

Remember though, this is an unconventional thing to do.

This doesn't bother me much, but there will be some people who find this disgusting and some sort of anti-pattern. This means that something that happens in the database layer could have some effect on the HTTP layer of your application.

Side effectsĪs mentioned a second ago, Laravel will pick up on the ValidationException and try to redirect the user back with some errors. GET /posts and POST /posts would be fine. If for whatever reason your previous location was the same as the route where validation is failing, you will probably get stuck an a redirect loop.īe sure to only use this auto-validation pattern on separate routes, i.e. Since the Validator::validate() method throws a ValidationException when validation fails, Laravel will try to catch that exception and redirect back to the previous location. Forget about the boring stuff and enjoy the magic. This approach will mean you only have to do is in one place, just create your models as regular elsewhere. You won't need to go through all of your controllers / form requests and change them. As long as you know the name of the method (use some sort of interface if you want), your controller won't know anything about the rules present, just that it needs to do some sort of validation.Ī smaller point is that any changes to the rules will be applied everywhere. This is another point on top of my last one. Re-usabilityĮven if you didn't want to do the whole auto-validation thing, you could still place your rules on the model and use those elsewhere. Your model handles all of that and encapsulates it in a single place. Your controller no longer needs to do any validation or worry about what data is being validated. With this auto-validation pattern, that is easy enough to achieve.
Laravel eloquent create validate request full#
In Laravel, this means your model becomes a God class full of database knowledge, and your controllers simply call methods without knowing what columns are present and what data types they are. One common thing, especially in more domain-driven patterns, is ensuring that your HTTP layer knows as little as possible about your database. Now when the $post->toArray() call is made, the published_at column will be present and processed.

Most Laravel developers have used model events at some point in their career. Have you ever considered removing that logic from your main request / response flow and holding the model accountable instead? If not, let me show you how it can be done and some of the pros and cons. You could even do it inside of your middleware if you really wanted to. You can do it inside of FormRequest objects or as part of your controller logic. User's existence will be only checked via email, but when created, the new record will save both email and name.In most applications validation logic is placed in standardised places. Which means you can use it like this: User::firstOrCreate(, ) In Laravel 5.3, the firstOrCreate method has the following declaration: public function firstOrCreate(array $attributes, array $values = ) The firstOrCreate is available in Laravel 5x, the answer is too old and it was given for Laravel-4.0 in 2013. Instead of creating a static method, you can use a scope in the Model, so the method in the Model will be scopeMethodName and call Model::methodName(), same as you did in the static method, for example $user = User::findOrCreate(5) This is another way to do the same thing: public function scopeFindOrCreate($query, $id) If a user with id of 5 exists, then it'll be updated, otherwise a new user will be created but the id will be last_user_id + 1 (auto incremented). There is already a method findOrFail available in Laravel and when this method is used it throws ModelNotFoundException on fail but in your case you can do it by creating a method in your model, for example, if you have a User model then you just put this function in the model // Put this in any model and useįrom your controller, you can use $user = User::findOrCreate(5) Below is the original accepted answer for: Laravel-4
