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

淺唱寂寞╮ 提交于 2019-12-24 17:43:15

问题


I got this error when I was trying to learn Laravel migrations from a video tutorial. The tutor created a file named Painting.php in the app/models folder. The content of Painting.php is:

<?php

    class Painting extends Eloquent{

    }
?>

and then in the routes.php:

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

    return view('welcome');
});

Now , question is where I am supposed to place Painting.php file since there is no models folder in Laravel 5.1?


回答1:


You need a namespace for Painting class:

<?php
namespace App;

class Painting extends Eloquent {}

And an use statement in routes.php:

<?php
use App\Painting;

This scenario assumes Painting.php resides in the app folder.




回答2:


The following changes made will make your code work -----------------------------------------------------------------------------

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


来源:https://stackoverflow.com/questions/31358101/fatalerrorexception-in-routes-php-line-22-class-painting-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!