mardi 20 octobre 2015

Laravel 5.1: boot function not called

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire