1

I have destroy function:

public function destroy(Tag $tag)
{
    $tag->delete();
    return Response::json([], ResponseHttp::HTTP_OK);
}

If frontend send one id of post - all ok. But how delete if frontend send array of post?

Georg
  • 107
  • 1
  • 10
  • You might want to try with a `foreach()` ? – Nirnae Aug 07 '17 at 07:27
  • You would have to create another route and (potentially) create another method. Please note that if you have any model events for deleting a model that you will have to delete them individually. – Rwd Aug 07 '17 at 07:27
  • 1
    I think you might want to read this thread: https://laracasts.com/discuss/channels/eloquent/how-to-delete-multiple-records-using-laravel-eloquent – online Thomas Aug 07 '17 at 07:27

4 Answers4

4

Try it,if you can get array of ids then you will delete them as

public function destroyArrayOfTag(array $tag){ //you can also use Request for getting post attributes

    Tag::whereIn('id',$tag)->delete();

    return Response::json([], ResponseHttp::HTTP_OK);
 
}

where $tag is a array of ids. Best of luck

1

I do it from filter -

public function destroy()
    {
        Tag::filter(\Request::only(['filter']))->delete();

        return Response::json([], ResponseHttp::HTTP_OK);
    }
Georg
  • 107
  • 1
  • 10
0

You could also pass an array of ids using a delimiter. eg. comma

So your route would look like this destroy/1,2,3,4

Then in your controller:

public function destroy($ids)
{
    Tag::destroy(explode(",",$ids));
    return Response::json([], ResponseHttp::HTTP_OK);
}

EDIT: You are using DELETE method, so your url would look something like this api/tag/1,2,3

CUGreen
  • 3,066
  • 2
  • 13
  • 22
  • It's return - [] and doesn't delete. – Georg Aug 07 '17 at 07:58
  • Are you using a resource route/controller or a custom route? Can you please provide your front end code? – CUGreen Aug 07 '17 at 08:01
  • Route::delete('/{tag}', 'TagController@destroy')->middleware('auth:api'); – Georg Aug 07 '17 at 08:03
  • Should work. Though you are better off using something like ```Route::delete('/tag/{id}'...``` then use a method ```DELETE``` on ```tag/1,2,3```. Can I see your front end script? – CUGreen Aug 07 '17 at 08:27
  • No, it's only for API. – Georg Aug 07 '17 at 08:28
  • Route::group(['namespace' => 'Tags', 'prefix' => 'posts'], function () { Route::delete('/{tag}', 'TagController@destroy')->middleware('auth:api'); }); – Georg Aug 07 '17 at 08:30
  • Ok, I need to see what url your are using, it should be something like ```api/tag/1,2,3``` where ```1,2,3``` are the ids you want to delete – CUGreen Aug 07 '17 at 08:43
  • Ok, it's start work, but delete only first element of array – Georg Aug 07 '17 at 09:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151253/discussion-between-cugreen-and-georg). – CUGreen Aug 07 '17 at 09:59
-1

perhaps you should add an array variable in your ajax. example like this :

    var data =[];

and then fetch the array post into data. and then you can do this thing :

    for(var i=0; i<data.length; i++)
    {

        $.ajax({
        url: your_path+'/destroy/'+data[i],
        type: 'GET',
        dataType: 'json',
        success:function(response) {
                         notify(1,"Your Response Message");
                    }
                })
    }

Hope this will help you. Thank's

Almaida Jody
  • 558
  • 2
  • 9
  • 19
  • 1
    This will not an ideal solution as this will create multiple AJAX call. What you need to do is pass the array of ids through AJAX an delete these on the destroy function by looping through each. – Amit Joshi Aug 07 '17 at 07:51