首页 > 解决方案 > 未定义的变量:routes/web.php LARAVEL 中的总数

问题描述

我在将变量hours从控制器传递到刀片模板时遇到问题。怎么了?

网页.php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/hours', 'easyController@hours');
//widok dodawania godzin
Route::get('/hours/add', 'easyController@add');
//zapisywanie do bazy danych
Route::post('/hours', 'easyController@save');
//usuwanie z bazy danych
Route::delete('/hours/{id}', 'easyController@delete');
//suma czasu pracy z bazy
Route::get('/hours', function(){
    return view('hours', ['total' => $total]);
});

控制方法:

public function pass(){
        $total = DB::table('hours')->select(DB::raw("SEC_TO_TIME( SUM( TIME_TO_SEC('total'))) as suma"))->value('suma');
        return view('hours', ['total' => $total]);
    }

hours.blade.php:

<p>{{$total}}</p>

标签: phplaravelvariablesundefined

解决方案


您应该将 web.php 路由文件更改为

Route::get('/hours', function(){
    $total = DB::table('hours')->select(DB::raw("SEC_TO_TIME( SUM( TIME_TO_SEC('total'))) as suma"))->value('suma');
    return view('hours', ['total' => $total]);
});

或者更好

Route::get('/hours', 'easyController@pass');

推荐阅读