Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel seeder出现错误,赢得';t种子_Php_Laravel_Laravel Seeding - Fatal编程技术网

Php laravel seeder出现错误,赢得';t种子

Php laravel seeder出现错误,赢得';t种子,php,laravel,laravel-seeding,Php,Laravel,Laravel Seeding,我一直试图让我的播种机工作,但它不断给我以下错误 Call to undefined function Database\Seeders\factory() at database/seeders/ContactTableSeeder.php:16 12▕ * @return void 13▕ */ 14▕ public function run() 15▕ { ➜ 16▕ factory(

我一直试图让我的播种机工作,但它不断给我以下错误

 Call to undefined function Database\Seeders\factory()

  at database/seeders/ContactTableSeeder.php:16
     12▕      * @return void
     13▕      */
     14▕     public function run()
     15▕     {
  ➜  16▕         factory('App\Models\Contact', 100)->create()
     17▕         ->each(function($contact) {
     18▕             $contact->addresses()->save(
     19▕                 (factory(App\Address::class)->make())
     20▕             );

      +24 vendor frames 
  25  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
我的数据库播种机

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        //Model::unguard(); // Disable mass assignment

        $this->call(ContactTableSeeder::class);

        //Model::reguard();
    }
}

替换工厂功能
工厂('App\Models\Contact',100)->create()

使用此代码:

\App\Models\Contact::factory()->count(100)
     ->create();

为什么??由于在Laravel8中,默认路由命名空间已在Laravel8中删除,因此您需要像下面这样更新工厂文件,或将
laravel/legacy Factorys
包添加到项目中。进一步资料:


在laravel 8中,默认路由命名空间被删除

尝试改变


没有关系。。。操作员错误…lol它正在工作!!非常感谢你!!!
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Models\Contact;
use Faker\Generator as Faker;

$factory->define(Contact::class, function (Faker $faker) {
    return [
            'firstName' => $faker->firstName,
            'lastName' => $faker->lastName,
            'email' => $faker->unique()->email,
            'phone' => $faker->phoneNumber,
            'birthday' => $faker->date($format = 'Y-m-d', $max = 'now')
        ];
    });
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Models\Address;
use Faker\Generator as Faker;

$factory->define(Address::class, function (Faker $faker) {
    return [
        'number'    => $faker->number,
        'street'    => $faker->streetName,
        'city'      => $faker->city,
        'state'     => $faker->state,
        'zip'       => $faker->postcode,
        'type'      => 'home',
        'contact_id'=> factory(App\Models\Contact::class),
    ];
});
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use HasFactory;

    protected $fillable = [ 'firstName', 'lastName', 'email', 'phone', 'birthday' ];

    public function addresses()
    {
        return $this->hasMany('App\Models\Address');
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    use HasFactory;
    protected $fillable = [ 'id', 'number', 'street', 'city', 'state', 'zip', 'type', 'contact_id' ];

    public function contacts()
    {
        return $this->belongsTo('App\Models\Contact');
    }
}
\App\Models\Contact::factory()->count(100)
     ->create();
<?php

namespace Database\Factories;

use App\Models\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Contact::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'firstName' => $this->faker->firstName,
            'lastName' => $this->faker->lastName,
            'email' => $this->faker->unique()->email,
            'phone' => $this->faker->phoneNumber,
            'birthday' => $this->faker->date($format = 'Y-m-d', $max = 'now')
        ];
    }
}
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class ContactTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        App\Models\Contact::factory()->create(100)
            ->each(function($contact) {
                $contact->addresses()->save(
                    App\Address::factory()->make()
                );
        });
    }
}
  factory(App\Models\Contact::class,100)->create();
\App\Models\Contact::factory()->create(); 
\App\Models\Contact::factory(100)->create(); \\If you want to create 100 number of record then