Does firstOrCreate respects casts?

Viewed 93

I have a tag model with the following:

$table->string('name')->unique();
$table->string('slug')->unique();

I am using firstOrCreate():

$name = "test tag 1";
Tag::firstOrCreate(['name' => $name, 'slug' => $name])

An inbound cast is handling the slug before inserting it. This works fine for the first time only. It seems like it only uses the cast for inserting, not for checking if it exists.

Inserting:

insert into
  "tags" ("name", "slug", "updated_at", "created_at")
values
  (
    'test tag 1',
    'test-tag-1',
  )

Checking:

select
  *
from
  "tags"
where
  (
    "name" = 'test tag 1'
    and "slug" = 'test tag 1'
  )

Is there something I could so it would also use the cast when checking with the firstOrCreate() method?

0 Answers
Related