hsntngr
240
30
Laravel

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

Posted 8 months ago by hsntngr

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 ?

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.