I'm having trouble removing an object inside of this array.
First I get the $id that is to be removed from the array.
But when I'm filtering throw the array its appending keys to it.
So the logic ll no longer work on the rest of the application.
How can I maintain the same syntax on the options object after removing the object inside of the cart array ?
public function destroy( $id, Request $request )
{
$user = $this->user ;
$data = array_filter( $user->options->cart , function ( $option ) use ( $id ) {
if ( $option->product_id == $id ) {
return false;
}
return json_encode($option);
});
//dd($user->options->cart);
//dd($data);
$user->options = (object)['cart' => $data ];
$user->save() ;
return response()->json( $user , 200 ) ;
}
Solved :
public function destroy( $id, Request $request )
{
$user = $this->user ;
$data = array_filter( $user->options->cart , function ( $option ) use ( $id ) {
if ( $option->product_id == $id ) {
return false;
}
return true;
});
$user->options = ['cart' => array_values( $data ) ];
$user->save() ;
return response()->json( $user , 200 ) ;
}
}
