nero

How to use the correct updateOrcreate in Laravel?

Posted 8 months ago by nero

How to use the correct updateOrcreate in Laravel?

I want to add and change data in the same code. I've been using updateOrcreate but the results are not as desirable.

The code can only change the data, but can not change the data.

My table:

jobs:

    id
    title
    deskription

educations:

    id
    name

job_educations:

    id
    job_id
    education_id

Here's my code snippet:

    public function update(Request $request, $id)
      {                
        $input = Input::all();
        $edu=$input ['education'];
            for($i=0;$i<count($edu);$i++){
                JobEducation::updateOrCreate(
                    [
                            'job_id'=>$id, 
                     ],
                     [
                         'job_id'=>$id,                
                            'education_id' => $edu[$i]                   
                    ]
             );
    }

My requirement: I want to change the existing data and add new data.

Can anyone fix my code?

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.