我正在尝试填充一个laravel 5.6项目数据库-遵循
offial docs
-没有成功。
php artisan db:seed
引发此异常:
Symfony\Component\Debug\Exception\FatalThrowableError:未找到类“App\Item”
at/Applications/MAMP/htdocs/greylab/inventario/greylab-inventario/vendor/laravel/framework/src/Illuminate/Database/elokent/FactoryBuilder.php:217
异常跟踪:
1说明\数据库\雄辩\ FactoryBuilder::make([])
/应用程序/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/elokent/FactoryBuilder.php:167
2说明\数据库\雄辩\ FactoryBuilder::创建()
/应用程序/mamp/htdocs/greylab/inventario/greylab_inventario/database/seeds/itemtableseeder.php:14
我已经尝试了社区提供的大多数常见建议,比如
this one
,以及:
-
尝试使用
composer self-update
+
composer dump-autoload
(二)
-
关于我的
composer.json
这个
autoload
属性设置为:
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
(也尝试将类映射放入自动加载开发程序中)。
情况是这样的:
项目工厂.php
<?php
use Faker\Generator as Faker;
$factory->define(App\Item::class, function (Faker $faker) {
return [ ...]
}
itemtableseeder.php项目表编辑器
<?php
use Illuminate\Database\Seeder;
class ItemTableSeeder extends Seeder
{
public function run()
{
factory(App\Item::class, 25)->create();
}
}
数据库播种器.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(ItemTableSeeder::class);
}
}
use App\Item;
use Illuminate\Database\Seeder;
通过移除
App\
前缀和仅保留
Item::class
在辩论中:
factory(Item::class, 25)->create();
所有这些尝试都没有帮助,所以我真的被卡住了。
如果有人能给我指路,我将不胜感激。
提前感谢大家。
更新
@kerbholz&@h-h:
ItemTableSeeder.php
,谢谢你的建议。是的,首先我实现了
Item.php
这样的模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Item extends Model
{
use SoftDeletes;
protected $table = 'item';
protected $dateformat = 'Y-m-d';
protected $fillable = [
'data_acquisto',
'labeled',
'estensione_garanzia',
'stato',
'data_dismissione',
'note'
];
protected $hidden = [
'codice',
'serial',
'componente_id',
'tipologia_id',
'condizione_id',
'locazione_id',
'fornitore_id',
'parent_id'
];
protected $dates = [
'data_acquisto',
'data_dismissione',
'deleted_at'
];
protected $touches = [
'componenti',
'condizioni',
'fornitori',
'locazioni',
'tipologie'
];
public function scopeFigli($query)
{
return $query->where('parent_id', '!=', null);
}
public function componenti()
{
return $this->belongsTo('App\Componente');
}
public function condizioni()
{
return $this->belongsTo('App\Condizione');
}
public function fornitori()
{
return $this->belongsTo('App\Fornitore');
}
public function locazioni()
{
return $this->belongsTo('App\Locazione');
}
public function tipologie()
{
return $this->belongsTo('App\Tipologia');
}
}
与此同时,我继续实施其他措施现在,在纠正了错误的类型并每年运行两次之后
作曲家转储自动加载
开始播种。它填充了一些表,但之后抛出了一个新的异常。以下是最后一次尝试的摘录:
Seeding: ItemTableSeeder
ErrorException : Illegal offset type
at /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:257
253| * @throws \InvalidArgumentException
254| */
255| protected function getRawAttributes(array $attributes = [])
256| {
257| if (! isset($this->definitions[$this->class][$this->name])) {
@H-H:在这种情况下,我试图在“app”之前加反斜杠:
\App\Item::class
没有成功不知道是不是和
faker
配置错误。。。