mercredi 8 mai 2019

Create token when model is created

I have a model with a user_id and a token column.

To create a mode I only pass a user_id like this:

$car = Car::create([
        'user_id' => $user->id,
]);

this is my car class:

class Car extends Model
{
    protected $guarded = [];

    public function __construct()
    {
      parent::__construct();
      $this->token = mb_substr(bin2hex(openssl_random_pseudo_bytes(32)),0,8);
    }

When I create a car, the token field is correctly inserted. However the user_id field is null. Why is the user_idempty?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire