在laravel中删除一行后无法获取数据

时间:2019-02-21 19:20:09

标签: laravel laravel-5 eloquent

我在laravel中创建了todo应用,其中有一个删除按钮。每当我添加数据时,它就会起作用,并显示在表格中。但是单击删除按钮后,其他数据将不在表中。

enter image description here

ToDosController

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

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

2 个答案:

答案 0 :(得分:0)

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

您什至没有到那里的数据,将其替换为:-

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

,并在获取每个变量后尝试转储并查看其是否为预期数据。

为什么要使用whereIN,而仅将其作为用户ID的1值表示作为用户表外键的唯一值?只是使用where子句?

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

这应该有效。

答案 1 :(得分:0)

通过调用Auth :: user并用where替换whereIn来解决问题。

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