Answer: I have used create() instead of insert() it should avoided the issue.Whenever we use Create function it inserts the only values given in $fillable fileds in model so it avoids all unwanted columns in the list
Original Question
I am trying to move values from temp table to main table after approval.
But, I don't have status, cancelled, and cancelled by columns in main table it's only available in the temp table. So, if I try to move all the fields from temp table to main table, it's showing error.
I am trying to move using the below codes.
public function approve(mpd_temp $mpd)
{
$result = mpd::insert($mpd->toArray());
if ($result) {
mpd_temp::where('sno', $mpd->sno)->delete();
toast('MPD Successfully Approved', 'success');
}
}
I have tried
$mpd->forget('status');
$mpd->forget('cancelled_at');
$mpd->forget('cancelledby');
Is there any best way to move columns from temp table to main table and delete from temp after insert?
You must unset()
the key you want to remove. In your case you must do
unset($mpd->cancelledby);
And it should be removed from the collection.