Whitney W. Whitney W. - 3 months ago 14
PHP Question

Use delete glyphicon in Laravel 5 to delete a data

Hi need help in glyphicons with delete functionality.
These are my icons for update and delete. However, I only have my update working. I do not do with my delete. Please help! THanks a lot!

<a href='{{ url("admin/editTicket/$ticket->id") }}'><span class="glyphicon glyphicon-pencil"></span></a>
<a href="#"><span class="glyphicon glyphicon-trash"></span></a>


Controller codes for update ticket:
where Chap_ticket is my database table name

public function editTicket($id)
{
$tick = Chap_ticket::find($id);
$tickets=Chap_ticket::all();
return view('admin.registerTicket',compact('tick','tickets'));

}


My Route:

Route::get('admin/editTicket/{id}','Admin\AdminController@editTicket');
Route::get('admin/deleteTicket', 'AdminController@deleteTicket');
Route::get('admin/registerTicket','Admin\AdminController@registerTicket');

Answer

You need to create a new controller action like e.g. below:

public function deleteTicket($id) { 
    $tick = Chap_ticket::find($id);
    $tick->delete(); 
    return Redirect::back()->with('msg', 'Ticket deleted'); 
}

You may need to associate the new action with a route so you can add the following in your routes:

Route::get('admin/deleteTicket/{id}', 'Admin\AdminController@deleteTicket');

Assuming your controller is called AdminController

And in your view:

@if (isset($msg))
    <div>
       {{$msg}}
    </div>
@endif

<a href='{{ url("admin/editTicket/$ticket->id") }}'><span class="glyphicon glyphicon-pencil"></span></a>
<a href="{{ url("admin/deleteTicket/$ticket->id") }}"><span class="glyphicon glyphicon-trash"></span></a>

This is based on the assumption that your view is a general admin page which will still be valid to when the ticket is deleted.