Dr. MAF
Dr. MAF

Reputation: 1985

How to define a route for a link to go to destroy() function in Laravel 4?

I defined a resource in my routes like this:

    Route::resource('shops', 'shopsController');

I need to create a link to delete an item

<a class="btn btn-xs btn-danger" href="{{ URL::to('shops/'. $row->id . '/destroy') }}" > </a>

but this URL requires me to define a new route like this:

Route::get('shops/{id}/destroy', 'shopsController@destroy');

So, how can I enforce the URL to go through the default route, through its resource, to get the destroy function??

I tried

href="{{ route('shops.destroy', $row->id ) }}" data-method="delete" 

but I redirects me to show() instead!!!!

Upvotes: 0

Views: 2188

Answers (2)

Basheer Kharoti
Basheer Kharoti

Reputation: 4302

You can't delete the user via anchor tag href attr you need to delete it either with form or use Ajax.

   {{ Form::open(array('url' => 'shops/'. $row->id)) }}
                    {{ Form::hidden('_method', 'DELETE') }}
                    {{ Form::submit('Delete this User', array('class' => 'btn btn-warning')) }}
                {{ Form::close() }}

Here's the similar question

Update

Hope this would help you.

You can pass $row->id to the anchor Id attr like so

 <a class="btn btn-xs btn-danger" id="{{$row->Id}}" onclick="delete_user(this.id) return false;" href="#" rel="nofollow" >Delete this entry</a>

and then use the below script to work with Destory method.

function delete_user(Id) {
var result = confirm("Want to delete?");
if (result==true) {
        $.ajax({
            url:'http://localhost:81/laravel/myapps/public/shops/'+Id,
            type:"Post",
            data: {'_method':'delete'},
            success:function($msg){
                alert($msg);
            }
        });
    }
    }
</script>

Upvotes: 4

Saurabh
Saurabh

Reputation: 101

Try this

 <a class="btn btn-xs btn-danger" href="{{ route('shops.destroy',array($row->id)) }}" data-method="delete" rel="nofollow" data-confirm="Are you sure you want to delete this?">Delete this entry</a>

laravel-delete

Upvotes: 1

Related Questions