laravel - 在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
您可以在图像中看到它正在显示待处理的待办事项,已完成的待办事项,但它没有在表格中显示它们。
解决方案
$completed = ToDo::where('status', 'Completed');
您甚至没有在那里获取数据,请将其替换为:-
$completed = ToDo::where('status', 'Completed')->get();
并在获得每个变量后尝试转储它,看看它是否是预期的数据。
为什么使用 whereIN ,而它只有 1 个值作为 userid 意味着作为 users 表的外键的唯一值?只使用where子句?
$arr = ToDo::where('user_id', '=', $userid)->paginate(5);
这应该有效。
推荐阅读
- javascript - 有没有办法检查每个超时的游戏结束条件,但调用主函数通过单个函数重新启动游戏
- python - 我在表中有一个值,但是当我尝试验证该值是否存在时,它返回 false
- c++ - 链接描述文件中断程序
- javascript - 选择元素未附加 addEventListener
- python - 如何使我的功能不能与另一个功能同时工作?
- sas - 如果变量的条件为真,SAS 计数并汇总
- vba - 使用 VBA 将幻灯片的所有图片保存为单个 JPG 图像
- python-3.x - 对目录中的所有文件运行命令
- rust - 可变字符串的寿命不足以执行 for 循环
- javascript - localStorage 不会在刷新时保留数据