Jim E Russel
Jim E Russel

Reputation: 495

Laravel API - updateOrCreate

I'm using the updateOrCreate for storing the data from my dynamic field. The update works but I'm having an error when adding a new row.

public function store(Request $request) {
    foreach($request->rows as $row) {
      $db = Book::updateOrCreate(['id' => $row['id']]);
      $db->author_id = $request->author_id;
      $db->publisher_id = $request->publisher_id;
      $db->title = $row['title'];
      $db->genre = $row['genre'];
      $db->save();
    }
  }

I tried this method but now it doesn't work for both update and create.

public function store(Request $request) {
    foreach($request->rows as $row) {
      $db = Book::updateOrCreate(
      ['id' => $row['id'],
      [
        'title' => $row['title'];
      ]
      ]);
    }
  }

Error message enter image description here enter image description here

Upvotes: 0

Views: 3157

Answers (3)

ZeroOne
ZeroOne

Reputation: 9117

updateOrCreate

You may also come across situations where you want to update an existing model or create a new model if none exists. Laravel provides an updateOrCreate method to do this in one step. Like the firstOrCreate method, updateOrCreate persists the model, so there's no need to call save():

updateOrCreate(array $attributes, array $values = [])

Its should be like this. But make sure you have fillable in model

Option1

//Option 1
foreach($request->rows as $row) {
    $db = Book::updateOrCreate(
        [
            'id' => $row['id'] ?? null
        ],
        [
            'author_id' => $request->author_id,
            'publisher_id' => $request->publisher_id,
            'title' => $row['title'],
            'genre' => $row['genre']
        ]
    );
}

Options2

// Options 2
foreach($request->rows as $row) {
    $input = [
         'author_id' => $request->author_id,
         'publisher_id' => $request->publisher_id,
         'title' => $row['title'],
         'genre' => $row['genre']
    ];
    if (isset($row['id'])) {
        if($db = Book::find($row['id'])) {
            $db->update($input);
            continue;
        }
    }

    Book::create($input);
}

EDIT:

The errors shows that your routes didnt accept POST method.. change it to POST method

Ex: Route::post() instead of Route::get()

Upvotes: 2

Mahdi
Mahdi

Reputation: 216

Be sure that you used the Models in your controller

use App\ExampleModel;

write the protected fillable fields in your model like this:

protected $fillable = [
        'title','genre', ...
    ];

Upvotes: 0

TsaiKoga
TsaiKoga

Reputation: 13394

updateOrCreate(array $attributes, array $values = [])

Create or update a record matching the attributes, and fill it with values.

So your parameters is not correct. It needs two array parameters, not one array. Check this out: updateOrCreate

public function store(Request $request) {
    foreach($request->rows as $row) {
      $db = Book::updateOrCreate(
      ['id' => $row['id']],
      [
        'title' => $row['title']
      ]);
    }
  }

Upvotes: 1

Related Questions