我在app
根目录中创建了模型类。由于laravel 5使用app
根目录作为模型。我没有为Models
创建另一个文件夹。
class Posts extends Eloquent{}
在routes.php
:
Route::get('/', function () {
$painting = Posts;
$painting->title = 'Do no Wrong';
$painting->artist = 'D. Do right';
$painting->year = 2014;
$painting->save();
return view('welcome');
});
它会抛出以下错误:
Class 'Painting' not found
答案 0 :(得分:0)
将$painting = Posts;
更改为$painting = new Posts;
此外,我认为您应该像这样创建Eloquent模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
而不是试图扩展' Eloquent'。
答案 1 :(得分:0)
您可以使用create method
保存记录Posts::create([ 'title' => 'Do no Wrong', 'artist' = 'D. Do right']);
或者只是你可以实例Posts类并使用你的代码。
Route::get('/', function () {
$painting = new Posts;
$painting->title = 'Do no Wrong';
$painting->artist = 'D. Do right';
$painting->year = 2014;
$painting->save();
return view('welcome');
});
答案 2 :(得分:0)
您缺少名称空间和新语句;
尝试:
Route::get('/', function () {
$painting = new App\Posts;
$painting->title = 'Do no Wrong';
$painting->artist = 'D. Do right';
$painting->year = 2014;
$painting->save();
return view('welcome');
});
您的模型应如下所示:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model{
}