如何在Laravel 5中正确设置模型并修复我的错误


How can I correctly setup models in Laravel 5 and fix my error?

我目前正在学习Laravel 5。我已经连接了我的数据库。设置路线、创建控制器、创建视图并尝试创建模型,这正是我需要帮助的地方。

我使用php-artisan创建了我的模型,它位于/app目录中。

当我尝试在浏览器上访问/myer时。我得到以下错误:

MyersController.php第20行出现致命错误异常:未找到类"App''Http''Controllers''Myer"

我已将编辑过的文件放在http://www.filedropper.com/help

我不知道哪里出了问题,我乱用"use",最终我得到的只是找不到Class。这开始摧毁我的灵魂。如果有人能帮助我,我将永远感激!!

文件

来自MyersController.php

public function index()
{
    $myers = Myer::all();
    return view('myers.index')->with('myers'.$myers);
}

从routes.php

Route::get('/myer/', 'MyersController@index');
Route::resource('myer','MyersController');

来自Myer.php

namespace App;
use Illuminate'Database'Eloquent'Model;
class Myer extends Model
{
//
}

来自index.blade.php

   <h2>Myers</h2>
   <ul>
    @foreach ($myers as $list)
     <li>{{{ $list->name }}}</li>
    @endforeach
  </ul>

正如您在错误中看到的,它试图在与控制器相同的命名空间中找到模型:FatalErrorException in MyersController.php line 20: Class 'App'Http'Controllers'Myer' not found。在模型中,您可以看到它位于名称空间App中。

所以要么放

use App'Myer;

在名称空间下的控制器顶部,或者引用您需要的完整路径:

public function index()
{
    $myers = App'Myer::all();
    return view('myers.index')->with('myers'.$myers);
}

但是,如果您在这个控制器中更频繁地使用它,那么将它放在use中会更有效率。

附言:请不要让它摧毁你的灵魂。