Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有HTML表单的Laravel删除按钮_Php_Html_Forms_Laravel_Laravel 5 - Fatal编程技术网

Php 带有HTML表单的Laravel删除按钮

Php 带有HTML表单的Laravel删除按钮,php,html,forms,laravel,laravel-5,Php,Html,Forms,Laravel,Laravel 5,我使用的是HTML表单,而不是Laravel Collective 目前,我已经成功地为CMS中的用户创建了CRUD,但有一件事困扰着我: 如何在用户列表中设置删除按钮,而不是特定的编辑页面 此外,当用户点击删除按钮,显示删除特定用户的确认弹出窗口时,也会很好 下面是我的代码: 控制员: /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Res

我使用的是HTML表单,而不是Laravel Collective

目前,我已经成功地为CMS中的用户创建了CRUD,但有一件事困扰着我:

如何在用户列表中设置删除按钮,而不是特定的编辑页面

此外,当用户点击删除按钮,显示删除特定用户的确认弹出窗口时,也会很好

下面是我的代码:

控制员:

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $user = User::findOrFail($id);
    $user->delete();

    return redirect('/admin/users'); 
}
用户列表页面:

@extends('layouts.backend')

@section('content')
  <h1>Users</h1>
  <a class="btn btn-primary" href="/admin/users/create">Create new user</a>
  <table class="table">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Role</th>
        <th>Status</th>
        <th>Created</th>
        <th>Updated</th>
        <th>Operations</th>
      </tr>

    </thead>
    <tbody>
      @if($users)
        @foreach($users as $user)
          <tr>
            <td>{{$user->id}}</td>
            <td>{{$user->name}}</td>
            <td>{{$user->email}}</td>
            <td>{{$user->role ? $user->role->name : 'User has no role'}}</td>
            <td>{{$user->status == 1 ? 'Active' : 'Not active'}}</td>
            <td>{{$user->created_at->diffForHumans()}}</td>
            <td>{{$user->updated_at->diffForHumans()}}</td>
            <td>
              <a href="/admin/users/{{$user->id}}/edit" class="btn btn-primary">Edit</a>
              <a class="btn btn-danger" href="">Delete</a> // HOW TO ACHIEVE THIS?
            </td>
          </tr>
        @endforeach
      @endif
    </tbody>
  </table>

@endsection

您可以像这样更新代码:

<a class="btn btn-danger" href="/admin/users/{{$user->id}}/delete" >Delete</a>

您应该使用以下路径名称删除用户:

<a href="{{ route('admin.user.delete', [$user->id]) }}" class="btn btn-xs btn-danger" onclick="return confirm('Are you sure?')">Delete</a>

如您所述,当用户单击删除按钮以显示删除特定用户的确认弹出窗口时,这将非常好。为此,您必须使用
jquery
ajax
。更改以下代码:

<a class="btn btn-danger" href="">Delete</a>

从您发布的代码中不明显,但您的
DELETE
路线需要
DELETE
方法。应该如此

但是在您的列表中,您正试图使用
GET
方法访问它

实际上,您应该重用编辑页面中的代码,它已经伪造了
DELETE
方法

大概是这样的:

...
<td>
    <a href="/admin/users/{{$user->id}}/edit" class="btn btn-primary">Edit</a>
    <form method="POST" action="/admin/users/{{$user->id}}">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}

        <div class="form-group">
            <input type="submit" class="btn btn-danger delete-user" value="Delete user">
        </div>
    </form>
</td>
...

...
// Mayank Pandeyz's solution for confirmation customized for this implementation
<script>
    $('.delete-user').click(function(e){
        e.preventDefault() // Don't post the form, unless confirmed
        if (confirm('Are you sure?')) {
            // Post the form
            $(e.target).closest('form').submit() // Post the surrounding form
        }
    });
</script>
。。。
{{csrf_field()}}
{{method_field('DELETE')}
...
...
//Mayank Pandeyz的确认解决方案是为此实施定制的
$('.delete user')。单击(函数(e){
e、 preventDefault()//除非确认,否则不要发布表单
如果(确认('你确定吗?')){
//张贴表格
$(e.target).最近的('form').submit()//发布周围的表单
}
});
带有“laravel form helper”和jquery的选项

<div class="actions">
    <a href="#" class="list-icons-item delete-action">
        <i class="icon-trash"></i>
    </a>
    {{ Form::open(['url' => route('admin.users.destroy', $user), 'method' => 'delete']) }}
    {{ Form::close() }}
</div>


<script>
    $(document).ready(function () {
        $('.delete-action').click(function (e) {
            if (confirm('Are you sure?')) {
                $(this).siblings('form').submit();
            }

            return false;
        });
    });
</script>

{{Form::open(['url'=>route('admin.users.destroy',$user),'method'=>'delete'])}}
{{Form::close()}}
$(文档).ready(函数(){
$('.delete action')。单击(函数(e){
如果(确认('你确定吗?')){
$(this.this('form').submit();
}
返回false;
});
});

是的,我猜我必须使用AJAX来完成这项任务。好的,谢谢你的回答。干杯这不会起作用,因为我没有删除方法的路由,我不想为此创建页面。正如Mayank所回答的,最好使用AJAX。无论如何谢谢你!哇,这是最好的解决方案,而且很简单!我怎么没有想到在循环中直接使用表单……所以,我不需要AJAX。我已经试过了,效果很好!非常感谢你,戴夫!很乐意提供帮助:)另外还有一个比我更好的解决方案:)嗨,我也在尝试这种用户删除功能,但现在无法使用
@csrf
@method('delete')
<a class="btn btn-danger delete_user" href="javascript:void(0);" id="{{$user->id}}">Delete</a>
$('.delete_user').click(function(){
  if( confirm('Are you sure?') )
  {
    var id = $(this).attr('id');
    // Make an ajax call to delete the record and pass the id to identify the record
  }
});
...
<td>
    <a href="/admin/users/{{$user->id}}/edit" class="btn btn-primary">Edit</a>
    <form method="POST" action="/admin/users/{{$user->id}}">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}

        <div class="form-group">
            <input type="submit" class="btn btn-danger delete-user" value="Delete user">
        </div>
    </form>
</td>
...

...
// Mayank Pandeyz's solution for confirmation customized for this implementation
<script>
    $('.delete-user').click(function(e){
        e.preventDefault() // Don't post the form, unless confirmed
        if (confirm('Are you sure?')) {
            // Post the form
            $(e.target).closest('form').submit() // Post the surrounding form
        }
    });
</script>
<div class="actions">
    <a href="#" class="list-icons-item delete-action">
        <i class="icon-trash"></i>
    </a>
    {{ Form::open(['url' => route('admin.users.destroy', $user), 'method' => 'delete']) }}
    {{ Form::close() }}
</div>


<script>
    $(document).ready(function () {
        $('.delete-action').click(function (e) {
            if (confirm('Are you sure?')) {
                $(this).siblings('form').submit();
            }

            return false;
        });
    });
</script>