routes.php 第 22 行中的 FatalErrorException:Class 'Painting' 未找到

FatalErrorException in routes.php line 22: Class 'Painting' not found

我在尝试从视频教程中学习 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 文件放在哪里,因为 models 文件夹在 Laravel 5.1?

您需要一个命名空间 Painting class:

<?php
namespace App;

class Painting extends Eloquent {}

以及 routes.php 中的 use 语句:

<?php
use App\Painting;

此方案假定 Painting.php 驻留在 app 文件夹中。

进行以下更改将使您的代码正常工作 -------------------------------------------- ------------------------------

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');
});