routes.php第22行中的FatalErrorException:未找到类“绘画”

时间:2015-07-11 14:19:46

标签: php laravel-5 laravel-routing

当我尝试从视频教程学习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中没有 models 文件夹?

2 个答案:

答案 0 :(得分:3)

您需要Painting类的命名空间:

<?php
namespace App;

class Painting extends Eloquent {}

routes.php 中的使用声明:

<?php
use App\Painting;

此方案假设 Painting.php 位于 app 文件夹中。

答案 1 :(得分:0)

以下所做的更改将使您的代码正常工作 ----------------------------------------------- ------------------------------

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