首页 > 解决方案 > 如何从数据库中获取数据以在 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');

那么,我在这里做错了什么?有人可以解释一下吗?

标签: phplaravel-7

解决方案


将您的最后一条路线更改为

Route::get('/home', 'HomeController@getdata')->name('home');


推荐阅读