Reputation: 417
I have a html5 table which is dynamically made from database items and it contains links, e.g. delete icon, which is in link tags <a href="xxxx" class="yyyy"></a>
. I want to be able to click the delete icon and know, which item I want to delete. I have set class of the link the same as the relevant database items ID, but I cant read the class from the controller, after I click the link. Is there a method of doing that in PHP Laravel? Or maybe you could suggest a way better way to accomplish that? This seems a way off tactic for this.
Upvotes: 0
Views: 1320
Reputation: 2807
Format your links as: If for example you are listing all items using foreach:
@foreach( $items as $item )
<a href="{{URL::to('/item/delete/'.$item->id}}">{{$item->name}}</a>
@endforeach
Inside routes.php
Route::get('item/delete/{id}', 'ItemsController@deleteItem');
inside ItemsController.php define the following function
public function deleteItem($id) {
$item = Item::get($id);
if( !$item ) App::abort(404);
$item->delete();
return Redirect::to('/');
}
and I am assuming you have your model in Item.php
class Item extends Eloquent {
protected $table = 'items';
}
and your items table has id and name columns
Upvotes: 0
Reputation: 6392
If each row on the table represents a row on database, so your link could contain the id from database. For example, a user table.
Row 1 => Link /users/delete/1
Row 2 => Link /users/delete/2
Row 3 => Link /users/delete/3
By doing it this way, you can know for sure which one is called.
On your routes file, if you are not using Route::resource()
, you should have something like this:
Route::get('users/delete/{id}', 'UsersController@destroy');
And in your destroy
method:
public function destroy($id)
{
// your logic here
}
Upvotes: 1