0

I want to validate that my model is valid when I save or update it. So I've created a classe called EloquentValidating from which I extend the class for all my model. Here is my boot function in EloquentValidating:

public static function boot()
{
parent::boot();

static::creating(function($item)
    {
        if(!$item->isValid()) return false; 
    });
static::updating(function($item)
    {
        if(!$item->isValid()) return false;
    });

}

The problem I have is that boot is never called. I have put a breakpoint in it, and it never triger. So my model is not validating before saving.

I have another project using the same logic, and it is working. The only difference I can see is the fact that I have namespace in the other project and not in this one. So the failing class is defined as

use Illuminate\Database\Eloquent\Model;
class EloquentValidating extends Model {

while the working one is defined as:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class EloquentValidating extends Model {

Could the namespacing cause this kind of problem, and why??? If I have to introduce namespacing, I will, but I would like to understand why it is the cause.

thanks Benoit

1 Answer 1

1

Looks like you need to update compiled classes. Run

php artisan clear-compiled 

then...

php artisan optimize

If the problem persists, then do it manually removing the file bootstrap/cache/services.json, it will be generated automatically in the next request

Sign up to request clarification or add additional context in comments.

5 Comments

didn't worked. This system is fresh from GIT, so I had to do a composer update just before. So all the generated files are new.
ok, I've added the namespace, AND THEN did that, and then it worked.
Great! Glad to hear that
Does anyone have a clue why it worked after adding the namespaces? I had the same issue and adding namespaces did the trick. I have no clue why??
@RickBrunken, it is because you need to complain psr-4 declared in the composer.json of your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.