我当我试图从视频教程学习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没有模型文件夹?
你需要一个命名空间Painting
类:
<?php
namespace App;
class Painting extends Eloquent {}
而在routes.php文件的使用语句:
<?php
use App\Painting;
这种情况下假定Painting.php驻留在应用程序文件夹中。
下面所做的更改将会使你的代码工作 ----------------------------------------- ------------------------------------
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');
});