php - Undefined offset: 2 when I delete an element of the array and I want to return the rest of them

703

I am getting this error whenever I try to delete and element in the array. It only work when I delete everything. But I want to delete one by one. this is delete function

 public function delete($id){

      $user_id = auth() ->user()->id;
      $card = travel_plan::all()->where('travel_id', $user_id);
      foreach($card as $cardId){
        $cardd = $cardId;
    }  //->where('city_name', $uo)->first();
      $cardd->delete();

  return redirect('/home')->with('success', 'Post Removed');

}

this is my delete button

 {!!Form::open(['action'=>['[email protected]','id' =>$cardd[]],'method'=>'POST','class'=>''])!!}

this is how I retrieved the id

 $cardd = array();
      $card = travel_plan::all()->where('travel_id', $user_id);
     foreach($card as $cardId){
          $cardd [] = $cardId ->id;
      }
109

Answer

Solution:

Delete thetravel_plan which has$id, and just get all the rest

travel_plan::where('travel_id', $id)->delete();
$cardd = travel_plan::where('travel_id', $user_id)->pluck('id')->first();

@if (!empty($cardd))
{!!Form::open(['action'=>['[email protected]','id' =>$cardd],'method'=>'POST','class'=>''])!!}
@endif

People are also looking for solutions to the problem: javascript - CSS on home page only

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.