Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

guidsen's avatar

Best approach on single and multiple deletion in laravel

I got a overview page where I can click a button to delete a single row or can selected multiple rows to be deleted.

Because laravel can only delete an item by sending it as a delete request, I am curious what's the best approach on this issue.

I really don't want to use ajax for this because I'm leveraging feedback messages etc with Session::flash.

It would be really great to do something like: item/1?action=destroy for the single row, so I can use a form for the multiple values to be deleted, because I can't nest a form for the single row.

I would love to hear your way of doing these things.

0 likes
9 replies
mikebronner's avatar

Why not make all the items you want to be able to delete selectable in a form by ID, i.e. a checkbox. You then submit the deletions as a form DELETE, then in your controller grab all the submitted IDs from the checkboxes, and do: Model::destroy(Input::get('checkboxes[]'));

Just spitballing here, but I think this should work. :)

1 like
vincej's avatar

@mikebronner

Heah Mike, I'm following your suggestion here and it appears to work, but I am getting one little detail wrong, so would like your input if possible.

In the controller receiving the checked boxes, I have successfully got the array of check boxes. I can see them by doing

public function destroy(Request $request)
{
dd($request);
} 

However, when I do your code: Crew::destroy(Input::get('checkboxes[]'));

I get nothing - just the white page of death.

The array off of $request looks like this:

Request {#37 ▼
  #json: null
  #sessionStore: null
  #userResolver: Closure {#224 ▶}
  #routeResolver: Closure {#233 ▶}
  +attributes: ParameterBag {#39 ▶}
  +request: ParameterBag {#38 ▼
    #parameters: array:3 [▼
      "_method" => "DELETE"
      "_token" => "AbDOKeew2ajR3OHcqh7LOXolf0w9E1c6WdOI75af"
      "delete" => array:3 [▼
        0 => "26"
        1 => "65"
        2 => "66"
      ]
    ]
  }
  +query: ParameterBag {#45 ▶}
  +server: ServerBag {#42 ▶}
  +files: FileBag {#41 ▶}
  +cookies: ParameterBag {#40 ▶}
  +headers: HeaderBag {#43 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/crews/%7Bcrews%7D"
  #requestUri: "/crews/%7Bcrews%7D"
  #baseUrl: ""
  #basePath: null
  #method: "DELETE"
  #format: null
  #session: Store {#108 ▶}
  #locale: null
  #defaultLocale: "en"
}

Many Thanks !

mikebronner's avatar

Hi @vincej ,

That appears to look good. What do you do after the delete? Are you redirecting or showing a view? Also, check your database if the selected items were actually deleted?

If you do have views or redirects set up and the records are being deleted, then its likely an issue with something after the Crew::destroy(xxx) call. if the records are not being deleted, it may be in a issue with the parameters passed into Crew::destroy(xxx).

vincej's avatar

@mikebronner

Hi Mike, thanks for coming back. And Many thank for your help !

when I do:

public function destroy(Request $request)
    {
        Crew::destroy(Input::get('checkboxes[]'));
        return redirect('crews/create');
    }

Nothing gets deleted. But i am not getting the white sheet of death.

If I do this:

 public function destroy()
    {

    $data = Input::all();
      foreach($data as $id) {
      DB::table('crews')->where('id', $id)->delete();
    }
     return redirect('crews/create');
    }

I can delete a single only. It does not appear to loop over the array, deleting each value. If I do a dd($data) I get this array:

array:3 [▼
  "_method" => "DELETE"
  "_token" => "T4NHMU1WtT8lkBhzZ0kA3lR3gNAitU4BkSyHSNe5"
  "checkboxes" => array:2 [▼
    0 => "31"
    1 => "32"
  ]
]
vincej's avatar

@mikebronner

Heah Mike - you put me on the right path, got it to work. It would have been cleaner if I could have done it in Eloquent though.. No matter:

    public function destroy()
    {
    $data = Input::all();

    foreach($data['checkboxes'] as $id) {
    DB::table('crews')->where('id', $id)->delete();
    }
        return redirect('crews/create');
    }
1 like
RachidLaasri's avatar

@vincej

I think there's a better ways to do it, rather than sending N queries. why don't you use "IN" ?

delete FROM crews where id in (1,2,3)
vincej's avatar

HI @RachidLaasri

yes - doing a single query would be better.

However, Sorry I don't follow your meaning .. I would be guessing.

ARCANEDEV's avatar

@vincej If get this array when you do a dd(Input::all()) :

array:3 [▼
  "_method" => "DELETE"
  "_token" => "T4NHMU1WtT8lkBhzZ0kA3lR3gNAitU4BkSyHSNe5"
  "checkboxes" => array:2 [▼
    0 => "31"
    1 => "32"
  ]
]

So you can access the input values by doing this:

public function destroy(Request $request)
{
    $ids = $request->input('checkboxes');
    // OR : $ids = Input::get('checkboxes');

    Crew::destroy($ids);

    return redirect('crews/create');
}

Hope this help.

1 like
mikebronner's avatar

Ah, I think the issue was here that doing Input::get('checkboxes[]') did not return anything, and Input::get('checkboxes') returns the array we were looking for. Thanks @ARCANEDEV for the fix of the typo :)

(Like I said, it was just off the top of my head) :) haha

Please or to participate in this conversation.