【发布时间】:2018-12-27 12:22:54
【问题描述】:
我正在尝试填充 Laravel 5.6 项目数据库 - 遵循 offial docs - 但没有成功。 php artisan db:seed 抛出这个异常:
Symfony\Component\Debug\Exception\FatalThrowableError:找不到类“App\Item”
在 /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:217
异常跟踪:
1 Illuminate\Database\Eloquent\FactoryBuilder::make([]) /Applications/MAMP/htdocs/greylab/inventario/greylab_inventario/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:167
2 Illuminate\Database\Eloquent\FactoryBuilder::create() /Applications/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/"
}
},
(也尝试将 classmap 放入 autoload-dev)。
情况如下:
ItemFactory.php
<?php
use Faker\Generator as Faker;
// Definizione dati test
$factory->define(App\Item::class, function (Faker $faker) {
return [ ...]
}
ItemTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class ItemTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Item::class, 25)->create();
}
}
DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
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
// Definizione Namespace
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Classe Item
*/
class Item extends Model
{
use SoftDeletes;
// Dichiarazione Proprietà
protected $table = 'item';
protected $dateformat = 'Y-m-d';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'data_acquisto',
'labeled',
'estensione_garanzia',
'stato',
'data_dismissione',
'note'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'codice',
'serial',
'componente_id',
'tipologia_id',
'condizione_id',
'locazione_id',
'fornitore_id',
'parent_id'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'data_acquisto',
'data_dismissione',
'deleted_at'
];
/**
* All of the relationships to be touched.
*
* @var array
*/
protected $touches = [
'componenti',
'condizioni',
'fornitori',
'locazioni',
'tipologie'
];
/**
* Scope query item figli
* Getter
* @param array $query Query
* @return array Query
*/
public function scopeFigli($query)
{
return $query->where('parent_id', '!=', null);
}
/**
* Componenti Correlati
* Getter
* @return object Componenti
*/
public function componenti()
{
// Definizione relazione
return $this->belongsTo('App\Componente');
}
/**
* Condizioni Correlate
* Getter
* @return object Condizioni
*/
public function condizioni()
{
// Definizione relazione
return $this->belongsTo('App\Condizione');
}
/**
* Fornitori Correlati
* Getter
* @return object Fornitori
*/
public function fornitori()
{
// Definizione relazione
return $this->belongsTo('App\Fornitore');
}
/**
* Locazioni Correlate
* Getter
* @return object Locazioni
*/
public function locazioni()
{
// Definizione relazione
return $this->belongsTo('App\Locazione');
}
/**
* Tipologie Correlate
* Getter
* @return object Tipologie
*/
public function tipologie()
{
// Definizione relazione
return $this->belongsTo('App\Tipologia');
}
}
与此同时,我继续实施其他人。现在,在更正错误类型并再次运行两次之后,composer dump-autoload 播种开始了。它填充了一些表,但之后引发了一个新异常。这是上次尝试的摘录:
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 配置错误有关...
【问题讨论】:
-
factory(App\Item::class, 25)->create();应该这样做。你有模型 App\Item 吗??