vendredi 19 février 2016

Laravel : Model saved with autogenerated uuid but instance of this model have this uuid empty

I use a trait for generate uuid on my model. It's work because, in my database the uuid value is not empty. But the current instance of my model don't have the new value.

this is the trait :

trait UuidModel
{
    public static function bootUuidModel()
    {
        static::creating(function ($model) {
            // Don't let people provide their own UUIDs, we will generate a proper one.
            $model->uuid = Uuid::generate(4);
            return true;
        });

        static::saving(function ($model) {
            // What's that, trying to change the UUID huh?  Nope, not gonna happen.
            $original_uuid = $model->getOriginal('uuid');

            if ($original_uuid !== $model->uuid) {
                $model->uuid = $original_uuid;
            }
            return true;
        });
    }

    public function scopeUuid($query, $uuid, $first = true)
    {
        if (!is_string($uuid) || (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid) !== 1)) {
            throw (new ModelNotFoundException)->setModel(get_class($this));
        }

        $search = $query->where('uuid', $uuid);

        return $first ? $search->firstOrFail() : $search;
    }

    public function scopeIdOrUuId($query, $id_or_uuid, $first = true)
    {
        if (!is_string($id_or_uuid) && !is_numeric($id_or_uuid)) {
            throw (new ModelNotFoundException)->setModel(get_class($this));
        }

        if (preg_match('/^([0-9]+|[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})$/', $id_or_uuid) !== 1) {
            throw (new ModelNotFoundException)->setModel(get_class($this));
        }

        $search = $query->where(function ($query) use ($id_or_uuid) {
            $query->where('id', $id_or_uuid)
                ->orWhere('uuid', $id_or_uuid);
        });

        return $first ? $search->firstOrFail() : $search;
    }
}

This is my model :

class Image extends BaseModel
{
    use UuidModel;

    protected $table = 'images';
    public $incrementing = false;
    public $timestamps = true;
    protected $guarded = array('id', 'timestamps', 'path');
    protected $visible = array('timestamps', 'uuid');
    protected $hidden = array('id', 'path');

    public function Item()
    {
        return $this->belongsTo('App\Models\Item', 'id', 'item_id');
    }

}

This is my Controller :

class ImageController extends Controller
{
    ...

    public function store(CreateImageRequest $request, $itemSlug)
    {
        $item = $this->getItem($itemSlug);

        $file = $request->file('image');
        $extension = $file->getClientOriginalExtension();
        $filename = Uuid::generate(4) . '.' . $extension;
        $filePath = $item->id . '/' . $filename;
        Storage::disk('local')->put($filePath,  File::get($file));

        $image = new Image();
        $image->path = $filePath;

        $image = $item->Images()->save($image);

        return Response::json($image, 201);
    }

    ...

}

And the response is always : {} but the uuid is not empty in my database.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire