php - 如何从数据库中获取数据以在 laravel 7 中查看页面
问题描述
我不会从数据库中获取数据并将它们作为表格显示在视图页面中。我尝试了很多方法,但都没有奏效。我还在 home.blade.php 中使用了成员添加表单作为模型,它工作正常。
这是我的 home.blade.php
<!-- show tasks -->
<div class="container-fluid">
<div class="container mt-4">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Task</th>
<th scope="col">Assigned Date</th>
<th socpe="col">Sign-off Date</th>
<th socpe="col">Edit/Delete</th>
</tr>
</thead>
@foreach($tasks as $task)
<tr>
<td>{{$task->id}}</td>
<td>{{$task->task}}</td>
<td>{{$task->assigned_date}}</td>
<td>{{$task->end_date}}</td>
<td>
<a href="/deletetask/{{$tasks->id}}" class="btn btn-danger">Delete</a>
<a href="/edittask/{{$tasks->id}}" class="btn btn-warning">Edite</a>
</td>
</tr>
@endforeach
</table>
</div>
</div>
<!-- end Show tasks -->
这是我的 taskController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\task;
class taskController extends Controller
{
public function store(Request $request){
$this->validate($request,[
'task'=>['required', 'max:100', 'min:5'],
'assignedDate' => ['required', 'date'],
'endDate' => ['required', 'date'],
]);
$task = new task;
$task->task = $request->task;
$task->assigned_date = $request->assignedDate;
$task->end_date = $request->endDate;
$task->save();
return redirect()->back()->with('message', 'Task added successfuly');
}
public function getdata()
{
$data=task::all();
return view('home')->with('tasks', $data);
}
}
这是我的 web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('/saveTask', 'taskcontroller@store');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
那么,我在这里做错了什么?有人可以解释一下吗?
解决方案
将您的最后一条路线更改为
Route::get('/home', 'HomeController@getdata')->name('home');
推荐阅读
- c - 两个单链表的联合和交集在C中递归地没有重复
- python - 在 Python 中将数据结构从对象列表更改为字典
- python - json.decoder.JSONDecodeError:期望值:尝试写入 json 文件时的第 1 行第 1 列(字符 0)
- javascript - 添加关于突出显示状态的图例和信息
- javascript - 我在 /store/AddProduct' 收到错误“MultiValueDictKeyError”。如何在不使用 request.POST.get() 的情况下解决它
- javascript - 如何在javascript中过滤对象子数组的数组和对象子数组的数组
- vb.net - VS 2019 - 如何升级 Visual Basic .NET 项目
- mongodb - 在我刷新数据库连接之前,Mongoose 不会获取数据
- graphql - 是否使用 GraphQL 查询或变异来发送外部消息
- autohotkey - 我的脚本在某些时候崩溃了,我已经消除了调试器能够发现的所有语法错误