tate
tate

Reputation: 49

updateOrCreate() not updating but adding new

Trying to update inserted data. But it's not quite working. If the data in the $variable changed then updateOrCreate method is just creating a new row. But it's supposed to be update the existed column?

$placeItems = [
    "website_id" => self::WEBSITE_ID,
    "url" => $place,
    "place_name" => $title,
    "image" => $image,
    "description" => "I am changing here",
];

Place::updateOrCreate($placeItems);

I changed the description column. And use updateOrCreate, but it's not update the id of description in. Insert it as new. The way i am using updateOrCreate is wrong?

UPDATED

I tried a way as mentioned in the answer below and test it.

$description = "new data";

$placeItems = [
    "website_id" => self::WEBSITE_ID,
    "url" => $place,
    "place_name" => $title,
    "image" => $image,
    "description" => "old data",
];

Place::updateOrCreate([
    "website_id" => self::WEBSITE_ID,
    "url" => $place,
    "place_name" => $title,
    "image" => $image,
    "description" => $description,
], $placeItems);

But it still inserting as new column. not updating the ID of description in.

Upvotes: 0

Views: 482

Answers (1)

Poldo
Poldo

Reputation: 1932

In your case you should specify parameters for updatedOrCreate.

The first parameter indicates the conditions for a match and second parameter specify which fields to update.

Place::updateOrCreate([ "website_id"=> self::WEBSITE_ID], [
     "url" => $place,
    "place_name" => $title,
    "image" => $image,
    "description" => $description]);

Read documentation here

Upvotes: 3

Related Questions