首页 > 解决方案 > 在laravel中删除一行后未获取数据

问题描述

我在 laravel 中创建了 todo 应用程序,其中有一个删除按钮。每当我添加数据时,它都会起作用,并显示在表格中。但是点击删除按钮后,其他数据没有出现在表格中。

在此处输入图像描述

待办事项控制器

public function index(Request $request)
    {
        $userid = $request->user()->todos()->get(['id']);
        $arr = ToDo::whereIn('user_id', $userid)->paginate(5);
        $count['counts'] = ToDo::whereIn('user_id', $userid);
        $pending = ToDo::where('status', 'Pending');
        $completed = ToDo::where('status', 'Completed');
        return view('admin.todo.index')->with('todos',$arr)->with($count)->with('pending',$pending)->with('completed',$completed)->with((array('user' => Auth::user())));
    }
 public function destroy($id) { 
   $todo = ToDo::find($id); 
   $todo->delete(); 
   return redirect()->route('admin.todo.index')->with('success', 'Deleted Successfully'); 
}

看法

<div class="box-body table-responsive no-padding">
              <table class="table table-hover">
                <tr>
                    <th>Status</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Schedule</th>
                    <th>Deadline</th>
                    <th>Action</th>
                </tr>
                @if(count($todos) > 0)
                @foreach($todos as $td)
                    <tr>
                        <td>
                            @if($td->status == 'Pending')
                            <span class="label label-danger">{{ $td->status }}</span></td>
                            @elseif($td->status == 'Completed')
                            <span class="label label-success">{{ $td->status }}</span></td>
                            @endif
                            </td>
                        <td>{{ $td->title }}</td>
                        <td>{{ $td->description }}</td>
                        <td>{{ $td->schedule }}</td>
                        <td>{{ $td->deadline }}</td>
                        <td>
                            <a href="{{route('admin.todo.edit',$td->id)}}" class="btn btn-info">Edit</a>
                            <a href="javascript:void(0)" onClick="$(this).parent().find('form').submit()" class="btn btn-danger">Delete</a>
                            <a href="{{ route('admin.todo.show',$td->id) }}" class="btn btn-info">Show</a>
                            <form action="{{route('admin.todo.destroy',$td->id)}}" method="post" value="DELETE">
                                {{ csrf_field() }}
                                {{ method_field('DELETE') }}
                            </form>
                            </td>

                    </tr>
                @endforeach

您可以在图像中看到它正在显示待处理的待办事项,已完成的待办事项,但它没有在表格中显示它们。

标签: laravellaravel-5eloquent

解决方案


 $completed = ToDo::where('status', 'Completed');

您甚至没有在那里获取数据,请将其替换为:-

 $completed = ToDo::where('status', 'Completed')->get();

并在获得每个变量后尝试转储它,看看它是否是预期的数据。

为什么使用 whereIN ,而它只有 1 个值作为 userid 意味着作为 users 表的外键的唯一值?只使用where子句?

$arr = ToDo::where('user_id', '=', $userid)->paginate(5); 

这应该有效。


推荐阅读