我试图填充offial docs之后的Laravel 5.6项目数据库,但没有成功。 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
之前,但没有成功。 Dunno,是否与某些faker
配置错误有关...
答案 0 :(得分:0)
您需要像这样导入Item
类:
use App\Item;
这意味着您可以执行以下操作:
factory(Item::class, 25)->create();
-
或者像这样将\
放在前面:
factory(\App\Item::class, 25)->create();
-
还请确保您的Item
类在顶部具有此名称:
namespace App;
答案 1 :(得分:0)
找到了。
在ItemFactory.php
内部,我将一个愚蠢的$this
作为工厂参数,用于创建关系:
$factory->define(App\Item::class, function (Faker $faker) {
[...]
'parent_id' => function() {
return factory($this)->create()->id;
}
}
通过以这种方式更改return
句子:
return factory(App\Item::class)->create()->id;
问题似乎已经解决。
感谢大家的帮助。