类App \ Http \ Controllers \ Panel不存在

时间:2018-04-04 11:53:10

标签: php laravel

我正在关注Laracasts系列,并在剧集Laravel 5.4 From Scratch: Route Model Binding上遇到了问题。

Laravel版本:

Laravel Framework 5.6.13

错误:

Class App\Http\Controllers\Panel does not exist

在/ panel和/ panel / 1页面上显示

应用\ HTTP \控制器\ PanelController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;

class PanelController extends Controller
{
    public function index()
    {
        $panels = Panel::all();
        return view('panel/index', compact('panels'));
    }

    public function show(Panel $panel)
    {
        return $panel;
        return view('panel/show', compact('panel'));
    }
}

路由/ web.php

// Main panel view
Route::get('/panel', 'PanelController@index');

// Individual panel view
Route::get('/panel/{panel}', 'PanelController@show');

应用/ Panel.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Panel extends Model
{
    public static function activePanels()
    {
        return static::where('status', 1)->get();
    }
}

3 个答案:

答案 0 :(得分:2)

在类

之前在面板控制器中添加此行
use App\Panel;

答案 1 :(得分:0)

您需要将use App\Panel;添加到课程顶部

或者通过完整名称空间$panels = App\Panel::all();

调用它

答案 2 :(得分:0)

您没有将模型包含在课堂中。

App\Panel添加到主要包含部分:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Panel;

class PanelController extends Controller
{
    public function index()
    {
        $panels = Panel::all();
        return view('panel/index', compact('panels'));
    }

    public function show(Panel $panel)
    {
        return $panel;
        return view('panel/show', compact('panel'));
    }
}

或手动加载类方法中的模型:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PanelController extends Controller
{
    public function index()
    {
        $panels = App\Panel::all();
        return view('panel/index', compact('panels'));
    }

    public function show(Panel $panel)
    {
        return $panel;
        return view('panel/show', compact('panel'));
    }
}