Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hsntngr's avatar

How to update the record if it exist or if not create new record

I want to create a draft if it doesn't exist. But if if there is a draft with the given post_key I want to update it, so..

 Post::updateOrCreate(
// if there is a record with the given key update it or create new one
           ['post_key' => $data['post_key']],
           [
            'title' => $data['title'],
            'slug' => str_slug($data['title']),
            'content' => $data['content'],
            'seo_title' => $data['seo_title'],
            'seo_desc' => $data['seo_desc'],
            'category_id' => $data['category_id'],
            'post_status' => 0,        
        ]);

But it doesn't work ?

0 likes
30 replies
Cronix's avatar

What does "it doesn't work," mean? What does it do instead? Are you sure you don't have multiple rows with the same post_key?

Vilfago's avatar

Do you have any error, or some other information to give ?

Cronix's avatar

And what happens? "it doesn't work" doesn't really tell us anything.

Cronix's avatar

what does your error log say then?

1 like
hsntngr's avatar

ohh I completely forgot that! -_-

hsntngr's avatar

it says slug doesnt have a default value @Cronix

General error: 1364 Field 'slug' doesn't have a default value (SQL: insert intoposts(title,content,seo_title,seo_desc,category_id,updated_at,created_at) values (title,content, , , 1, 2018-04-22 17:59:26, 2018-04-22 17:59:26))

Vilfago's avatar

So ypu have a "slug" column in your table, and you don't provide any value for it. (as per sql)

As you haven't set a default value in case of no value, neither allowing NULL, you have an error.

What it the "slug" value (or the result of str_slug($data['title']) ) ?

Strange that the slug is not in your query but seems good in your code. Did you change something?

Cronix's avatar

I'm assuming you have a form where this data is coming from. It looks like this has a "title" field, which gets translated into the slug. Did you submit a title?

Is the title field required in your validation rules?

hsntngr's avatar

@Cronix yes I did, also I submitted content and category id too, I can see them on console with console.log()

And yep, title field is required

Cronix's avatar

Well something strange is going on. Are you sure the code is exactly what you posted in your first post, and that you are actually hitting that code?

The query you showed that is getting produced doesn't match your original code. The query you showed doesn't even have a slug field.

Field 'slug' doesn't have a default value (SQL: insert intoposts(title,content,seo_title,seo_desc,category_id,updated_at,created_at) values (title,content, , , 1, 2018-04-22 17:59:26, 2018-04-22 17:59:26))

See? No "slug" field in the field names.

1 like
hsntngr's avatar

Yeah exactly the same codes.. Thats why I'm here, I can't figure out what's happening :/

Cronix's avatar

Put a dd($data) just before your updatedOrCreate and check the output.

1 like
hsntngr's avatar

also there is no post_status field too ..

Vilfago's avatar

Maybe try to fill the 2nd argument of str_slug

$slug = str_slug($data['title'], '-')
Cronix's avatar

Look in your browser tools in the network section. You can see all of the ajax requests, what they send, what url they go to, what is received back, the headers, etc.

bwrice's avatar
bwrice
Best Answer
Level 11

You are probably missing 'slug' from your $fillable attributes on the Model

1 like
AshishDhamala's avatar

You might not have specified "slug" field in your "$fillable" attribute in your model.

hsntngr's avatar

@bwrice you were right, I missed that -_- but still returns http status 500 without any error

bwrice's avatar

comment out the $fillable field on your Model and add

$guarded = [];

Then try and see if it still gives you a 500 status.

1 like
Cronix's avatar

Try what @bwrice suggested, and if that works, make sure all fields that you are using in your updateOrCreate are present in the $fillable array. You mentioned that post_status was missing too. Did you add that to $fillable?

1 like
jlrdw's avatar

From what I have seen if everyone would learn some JS and ajax outside of laravel first it would make things so much easier. There have been numerous discussions lately of folks having ajax trouble. Two things you can do to help:

  • Quit worrying about laravel techniques (update or create) etc.
  • Just use ajax in a traditional way

I.e., Just see if a record exist the normal way and if not create it. So much easier. Just one pre-query to check.

Those neat laravel shortcuts are great when not using ajax, but sometimes combined leads to a mess.

1 like
Cronix's avatar

what does the response say for the ajax request in your browser dev tools? When dealing with ajax, always check the browser tools in addition to the error logs. More than likely, the response is returning a whoops error page or something.

hsntngr's avatar

.... I'm sorry but I have to confess, I forgot the remove dd method.. it seems that updateOrCreate() method works as well, problem fixed after adding the protected $guarded = []; on model.

Cronix's avatar

Check your code for if you are still using dd() anywhere in the controller that the request is being sent to. That's the javascript that gets sent when you do that, so it's most likely what's going on.

1 like

Please or to participate in this conversation.