Laravel Save() Method Returning True But Not Updating Records

Laravel save() method returning true but not updating records

This line in the Opportunity model fixed the issue.

Protected $primaryKey = "opportunityID";

Although it is difficult to understand why it was still possible to retrieve the data and create a new record.

Laravel Model save() & update() Not Saving

You can try any of the following:

  1. Try using static update function instead

    PatronCoupons::update([
    // updates here
    ]);
  2. Remove your vendor folder and composer.lock file. Then run composer install to refresh your vendor file

PHP Laravel save() does not update record

Try to get an object instead of collection:

$pet = pets::find($pet['pet_id']);

if (!is_null($pet)) {
$update_pet->pet_breed_id = $pet_new['pet_breed_id'];
$update_pet->save();
}

Also, make sure you're getting the right object by putting dd($pet); right after the first line of the code.

How to return an updated data after updating in Laravel Model?

return as object instead of boolean type

public function updateStatus(Driver $driver)
{
$driver->update($this->validateStatus());
return $driver;// first way
// return tap($driver)->update($this->validateStatus()); //second way
}

public function validateStatus()
{
return $this->validate(request(), [
'status_id' => 'required|min:1|max:3'
]);
}

Laravel Model-save() returns false but no error

To get the insert queries when $user->save(); error, you can try to catch the exception like this:

    try{
$user = new User;
$user->fields = 'example';
$user->save(); // returns false
}
catch(\Exception $e){
// do task when error
echo $e->getMessage(); // insert query
}

Hope this helps :)

laravel database not being updated

You are updating the key used to find the record so it will now use this new key in the query which isn't the current key in the database. You need to use the original keys so it can find the record, then it can update the record with new values.

You would need to do something like this:

protected function setKeysForSaveQuery(Builder $query)
{
$number = $this->original['number'] ?? $this->getAttribute('number');
$type = $this->original['type'] ?? $this->getAttribute('type');

$query->where('number', '=', $number)
->where('type', '=', $type);

return $query;
}

This uses the original values for those keys if they exist and if not uses the current attribute value.



Related Topics



Leave a reply



Submit