Home > front end >  How to Remove Column from Laravel Collection variable
How to Remove Column from Laravel Collection variable

Time:06-28

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.

enter image description here

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');

But it's showing enter image description here

Is there any best way to move columns from temp table to main table and delete from temp after insert?

CodePudding user response:

You can use except function on your collection:

$mpd->except(['status', 'cancelled_at', 'cancelledby']);

CodePudding user response:

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.

  • Related