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?

3
  • You might want to try with a foreach() ? Commented Aug 7, 2017 at 7: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. Commented Aug 7, 2017 at 7:27
  • 1
    I think you might want to read this thread: laracasts.com/discuss/channels/eloquent/… Commented Aug 7, 2017 at 7:27

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

9 Comments

It's return - [] and doesn't delete.
Are you using a resource route/controller or a custom route? Can you please provide your front end code?
Route::delete('/{tag}', 'TagController@destroy')->middleware('auth:api');
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?
No, it's only for API.
|
1

I do it from filter -

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

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

Comments

-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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.