FatalErrorException in routes.php line 22: Class &

2019-08-08 03:38发布

问题:

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