Bastian
Bastian

Reputation: 1237

UpdateOrCreate not updating data - Laravel Eloquent

I am new to laravel. I have an issue when I am trying to update or create record in DB. I have a table called DspAccountFee with this columns: enter image description here

I want to create record of dsp_account_id + screen_type when the combination not exists, and to update if the combination exists. this is my code: (just tried to update the first row keys of -> dsp_account_id(5187) + screen type (ctv). However nothing changed.

DspAccountFee::updateOrCreate(
                ['dsp_account_id' => $dsp_account_id, 'screen_type' => 'ctv'],
                ['pmp_percent' =>$fields['fee_ctv_pmp_percent'], 'omp_percent' => $fields['fee_ctv_omp_percent']]
                );

When I print the values before the DB operation they exists:

\Log::info("dsp_account:");
\Log::info($dsp_account_id);
\Log::info("ctv pmp percent:");
\Log::info($fields['fee_ctv_pmp_percent']);
\Log::info("ctv omp percent:");
\Log::info($fields['fee_ctv_omp_percent']);
\Log::info("app pmp percent:");

enter image description here

What I am missing why it is not update the db? Nothing in logs and No exception

this is my method in the model

protected $fillable = array(
        'dsp_account_id', 'screen_type'
    );

Upvotes: 0

Views: 2800

Answers (1)

Benson Okello
Benson Okello

Reputation: 394

Check the corresponding model and make sure that those columns exist in the $fillable property. It should look somewhat like this.

  protected $fillable = [
  'dsp_account_id',
  'screen_type',
  'pmp_percent',
  'omp_percent'
];

Your updateOrCreate syntax looks okay.

To update the updated_at column in your database, you can use the touch() method: you'll need to edit your code to something like this

 $foo = DspAccountFee::updateOrCreate([
    'dsp_account_id' => $dsp_account_id,
    'screen_type' => 'ctv'
    ],
    [
    'pmp_percent' => $fields['fee_ctv_pmp_percent'],
    'omp_percent' => $fields['fee_ctv_omp_percent']
    ]);

   $foo->touch();

Upvotes: 2

Related Questions