FatalErrorException在routes.php文件线22:类“画”未找到(FatalE

2019-10-23 19:13发布

我当我试图从视频教程学习Laravel迁移此错误。 导师创建了一个在app / models文件夹命名Painting.php文件。 Painting.php的内容是:

<?php

    class Painting extends Eloquent{

    }
?>

然后在routes.php文件

Route::get('/', function () {
     $painting = new Painting; //**this thing generates error**
     $painting->title='Do no wrong';
     $painting->save();

    return view('welcome');
});

现在,问题是在哪里我应该把Painting.php文件,因为在Laravel 5.1没有模型文件夹?

Answer 1:

你需要一个命名空间Painting类:

<?php
namespace App;

class Painting extends Eloquent {}

而在routes.php文件的使用语句:

<?php
use App\Painting;

这种情况下假定Painting.php驻留在应用程序文件夹中。



Answer 2:

下面所做的更改将会使你的代码工作 ----------------------------------------- ------------------------------------

Painting.php

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Painting extends Model
{

}

/routes/web.php

<?php

use App\Painting;


Route::get('/', function () {

  $painting = new Painting;
  $painting->title = 'Its Your Wish';
  $painting->artist = 'Working Fine';
  $painting->year = 2017;
  $painting->save();

  return view('welcome');
});


文章来源: FatalErrorException in routes.php line 22: Class 'Painting' not found