untymage
3 months ago
227
7
Laravel

firstOrCreate duplicated entry

Posted 3 months ago by untymage

I have a table tags which have two records and have unique name column :

img

When i use firstOrCreate:

Tag::firstOrCreate(['name' => 'Laravel']);

Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Laravel' for key 'tags_name_unique' (SQL: insert into tags (name, updated_at, created_at) values (Laravel, 2020-02-24 06:47:55, 2020-02-24 06:47:55))'

I also try these:

Tag::firstOrCreate(['name' => 'Laravel', 'parent_id' => 1]);
Tag::firstOrCreate(['name' => 'Laravel'], ['parent_id' => 1])

But all of them throw a same error what is the problem ?

Please sign in or create an account to participate in this conversation.