简体   繁体   English

Laravel PHP,获得点击链接类

[英]Laravel PHP, getting clicked link class

I have a html5 table which is dynamically made from database items and it contains links, eg delete icon, which is in link tags <a href="xxxx" class="yyyy"></a> . 我有一个html5表,该表是由数据库项动态创建的,它包含链接,例如删除图标,该链接位于链接标签<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. 我已将链接的类设置为与相关数据库项目ID相同的链接,但是单击链接后,无法从控制器读取该类。 Is there a method of doing that in PHP Laravel? 在PHP Laravel中有做到这一点的方法吗? Or maybe you could suggest a way better way to accomplish that? 也许您可以提出一种更好的方法来实现这一目标? This seems a way off tactic for this. 这似乎是一种偏离策略的方法。

If each row on the table represents a row on database, so your link could contain the id from database. 如果表上的每一行代表数据库上的一行,那么您的链接可能包含数据库中的ID。 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::resource() ,则应具有以下内容:

Route::get('users/delete/{id}', 'UsersController@destroy');

And in your destroy method: 并在您的destroy方法中:

public function destroy($id) 
{
    // your logic here
}

Format your links as: If for example you are listing all items using foreach: 链接的格式设置为:例如,如果您要使用foreach列出所有项目:

@foreach( $items as $item )
<a href="{{URL::to('/item/delete/'.$item->id}}">{{$item->name}}</a>
@endforeach

Inside routes.php 内部routes.php

Route::get('item/delete/{id}', 'ItemsController@deleteItem');

inside ItemsController.php define the following function ItemsController.php内部定义以下函数

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 我假设您在Item.php中有您的模型

class Item extends Eloquent {
  protected $table = 'items';
}

and your items table has id and name columns 并且您的项目表具有id和name列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM