I have a Trait
that I use on multiple models in my app for setting a UUID
on the model:
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait UsesUuid
{
protected static function bootUsesUuid()
{
static::creating(function ($model) {
if (!$model->uuid) {
$model->uuid = (string) Str::orderedUuid();
}
});
}
}
It works fine when using the app normally, but when I try to write tests for creating the model via a post
route
and dump the response
I get an Integrity constraint violation: 19 NOT NULL constraint failed: venues.uuid
500
error.
An example of the test I'm doing is:
public function testOwnerCanSuccessfullyCreateVenue()
{
$amenity = Amenity::inRandomOrder()->pluck('id')->first();
$response = $this->actingAs($this->createdUser('owner'))->post(route('venues.store'), [
"name" => "create-name",
"address_line_1" => "create-address_line_1",
"address_line_2" => "create-address_line_2",
"county" => "create-county",
"postcode" => "create-postcode",
"phone_number" => "create-phone_number",
"notes" => "create-notes",
"amenities" => [$amenity]
]);
dd($response);
}
And the column in my migration is $table->uuid('uuid')->unique();
Everything else is working brilliantly, but I'm new to writing tests and not sure how to get around this problem.
The reason I was using a Trait was to get around outlining all the column values when persisting to the database, as I'm using $request->validated()
to fill the model:
$venue = Venue::create($request->validated());
And obviously because the UUID isn't being set here, it's being done by a Trait, its failing the test. I can get the test to pass and have the app still work if I remove the Trait and do:
$venue = Venue::create(
$request->validated() + ['uuid' => Str::orderedUuid()]
);
Which is fine, I can live with that, but I would like to understand more about why the Trait isn't firing and how to get around it.
Edit: Just to add more based on the answer below, the fillable fields are defined correctly:
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'uuid',
'name',
'address_line_1',
'address_line_2',
'county',
'postcode',
'phone_number',
'notes',
];