1

I'm trying to create a new row if there isn't one yet with the following condition:

['employee_id' => $id, 'competence_id' => $getCompetenceKey]

or if it exists already, I update it and set ['value' => $getCompetenceValue]

So I did this:

foreach($request->input('competence') as $getCompetenceKey => $getCompetenceValue) {
    EmployeeCompetence::updateOrCreate(
     ['employee_id' => $id,'competence_id' => $getCompetenceKey],
     ['value' => $getCompetenceValue]
    );
}

But it doesn't seem to work properly:

https://i.sstatic.net/8ndQt.png

I could do it like this though:

foreach ($request->input('competence') as $getCompetenceKey => $getCompetenceValue) {
    $e = EmployeeCompetence::where('employee_id', $id)->where('competence_id', $getCompetenceKey);
    if ($e->count() > 0) {
        $e->update(['value' => $getCompetenceValue]);
    } else {
            EmployeeCompetence::create(['employee_id' => $id,
                                      'competence_id' => $getCompetenceKey, 
                                              'value' => $getCompetenceValue]);
    }
}

But I really want to know why the updateOrCreate() function didn't work for me.

Thanks,

Kenny

3
  • What error laravel gives while updateOrCreate ?! Commented Oct 26, 2017 at 7:54
  • @HirenMakwana no error, just wrong query. Commented Oct 26, 2017 at 7:55
  • Thanks check my answer. Commented Oct 26, 2017 at 8:05

3 Answers 3

5

The first column is for update, but it should be also passed in the create function.

Example:

EmployeeCompetence::updateOrCreate(
    [
      'employee_id'   => $id, 
      'competence_id' => $getCompetenceKey
    ],
    [
      'value'         => $getCompetenceValue,
      'employee_id'   => $id, 
      'competence_id' => $getCompetenceKey
    ]
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment, but it doesn't seem to fix it. Got the same queries :(
0

I'm not sure this will work or not but as per my understanding.

updateOrCreate takes $primaryKey field as a query.

I'm not sure, may be this will solve your problem. Add below line in your model file and then try

protected $primaryKey = ['employee_id', 'competence_id'];

4 Comments

Getting: ErrorException (E_WARNING) Illegal offset type
Yes, Need to override some properties, You can find solutions here.. github.com/laravel/framework/issues/5355#issuecomment-161376267
Declaration of App\EmployeeCompetence::setKeysForSaveQuery(App\Builder $query) should be compatible with Illuminate\Database\Eloquent\Model::setKeysForSaveQuery(Illuminate\Database\Eloquent\Builder $query)
Have you set public $incrementing = false; ?!
0

I also faced issue with updateOrCreate method .

$user = user::updateOrCreate(
    ['email' =>  'xxx'],
    [
        'location' => 'yyy' ,
    ],
);

first I don't need to pass 'email' in 2nd element of argument. we need to create a fillable variable in model class with fields which we want to update .

protected $fillable = ['email' ,'location' ,'column1' ,'colun2'];

this worked for me so thought to post here.thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.