Reputation: 24059
I am either adding to my table or editing an existing row.
I've looked into firstOrCreate but I can't seem to get it to work, it always creates a new row.
return $this->firstOrCreate(array('id' => $input['id'], 'title' => $input['title'], 'sell' => $input['sell'], 'article' => $input['article'], 'thumb' => $input['thumb'], 'gallery' => $input['galleryData'], 'date' => $input['date'], 'published' => $input['published']));
Things like title change when the user edits them, is there a way to search the table based on the id, if it exists then update, if not, then create?
Upvotes: 0
Views: 1756
Reputation: 2647
If the title changes, then using this method is illogical. The method you mention check for an item with ALL the given specifications.
What you should be doing instead:
// find the item, given the ID $item = Item::firstOrNew(array('id' => $input['id'])); // add the fields from your input $item->title = $input['title']; $item->sell = $input['sell']; $item->article = $input['article']; $item->thumb = $input['thumb']; $item->gallery = $input['galleryData']; $item->date = $input['date']; $item->published = $input['published']; // Save the thing $item->save();
Upvotes: 2