How to return the replicated data instead of boolean in laravel

Viewed 54

I'm trying to replicate() eloquent data in laravel, and also return the replicated data instead of bool() data.

When I do this

$transaction = Transaction::find(1);

$transaction2 = $transaction->replicate()->fill(['status' => 1]);

$transaction2 = $transaction2->save();

return $transaction2;

This code returns true. How can I return the replicated data?

1 Answers

The result is happening because you are setting the $transaction2 to the save() result just before your return here $transaction2 = $transaction2->save();.

This will override $transaction2 with the value of ->save(), which returns true or false whether it successfully saved or not.

Change your code like this, and it will do as you intend.

$transaction = Transaction::find(1);

$transaction2 = $transaction->replicate()->fill(['status' => 1]);

$transaction2->save();

return $transaction2;
Related