Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 我如何编写一个在拉拉维尔8号有工厂的播种机? 我的问题_Php_Laravel_Laravel 8_Factories - Fatal编程技术网

Php 我如何编写一个在拉拉维尔8号有工厂的播种机? 我的问题

Php 我如何编写一个在拉拉维尔8号有工厂的播种机? 我的问题,php,laravel,laravel-8,factories,Php,Laravel,Laravel 8,Factories,我最近开始与Laravel 8合作。我正在尝试使用播种机和工厂生成测试数据。但是我总是会遇到错误,而且Laravel8的文档对于我的案例来说太差了。如果有人能帮我解决问题,我将非常感激 我的代码 用户工厂 用户播种机测试2 (注意:我还没有测试下面的代码) 在您的UserFactory中,我将使用名为email()、phones()和passwords()的方法定义状态。下面是电子邮件()的一个示例: 它将创建5个用户,每个用户有5个电子邮件地址。另外,在emails()状态方法中,您可以更新$

我最近开始与Laravel 8合作。我正在尝试使用播种机和工厂生成测试数据。但是我总是会遇到错误,而且Laravel8的文档对于我的案例来说太差了。如果有人能帮我解决问题,我将非常感激

我的代码 用户工厂 用户播种机测试2 (注意:我还没有测试下面的代码)

在您的UserFactory中,我将使用名为email()、phones()和passwords()的方法定义状态。下面是电子邮件()的一个示例:

它将创建5个用户,每个用户有5个电子邮件地址。另外,在emails()状态方法中,您可以更新$user记录,以便用户表中的“email”列与您的一个UserEmail记录匹配

如果您想创建一个具有5个电子邮件地址和2个电话号码的用户,您可以拨打:

public function run()
{
    \App\Models\User::factory()
                ->count(5)
                ->emails(5)
                ->phones(2)
                ->create();
}
<?php

namespace Database\Factories;

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

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            "name" => $this->faker->words(1),
            "email" => $this->faker->unique()->freeEmail(),
        ];
    }
}
<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * Tablename
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * PrimaryKey
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'firstname',
        'lastname',
        'birthdate',
        'birthplace',
        'taxnumber',
        'email',
        'password'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

    /**
     * Relation: One User has many UserEmails
     */
    public function useremails() {
        $this->hasMany(UserEmail::class);
    }
    
    /**
     * Relation: One User has many UserPasswords
     */
    public function userpasswords() {
        $this->hasMany(UserPassword::class);
    }
    
    /**
     * Relation: One User has many UserPhones
     */
    public function userphones() {
        $this->hasMany(UserPhone::class);
    }
}
<?php

namespace App\Models;

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

class UserEmail extends Model
{
    use HasFactory;

    /**
     * Tablename
     *
     * @var string
     */
    protected $table = 'user_emails';

    /**
     * PrimaryKey
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'name',
        'email'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
    ];


    /**
     * Relation: One UserEmail belongs to a User
     */
    public function user() {
        return $this->belongsTo(User::class);
    }
}
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

use App\Models\User;
use App\Models\UserEmail;
use App\Models\UserPassword;
use App\Models\UserPhone;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        for ($i = 0; $i < 100; $i++) {
            $user = User::factory()->create();
            
            $userEmail = UserEmail::factory()
                ->count(5)
                ->state(
                    function (array $attributes, User $user) {
                        return ['user_id' => $user->id];
                    }
                )
                ->create();
            $userPassword = UserPassword::factory()
                ->count(5)
                ->state(
                    function (array $attributes, User $user) {
                        return ['user_id' => $user->id];
                    }
                )
                ->create();
            $userPhone = UserPhone::factory()
                ->count(5)
                ->state(
                    function (array $attributes, User $user) {
                        return ['user_id' => $user->id];
                    }
                )
                ->create();
        }
    }
}
Database\Seeders\UserSeeder::Database\Seeders\{closure}(): Argument #2 ($user) must be of type App\Models\User, null given
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

use App\Models\User;
use App\Models\UserEmail;
use App\Models\UserPassword;
use App\Models\UserPhone;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        $user = User::factory()
            ->has(
                UserEmail::factory()
                    ->count(5)
                    ->state(function (array $attributes, User $user) {
                        return ['user_id' => $user->id];
                    })
                    ->create()
            )
            ->has(
                UserPassword::factory()
                    ->count(5)
                    ->state(function (array $attributes, User $user) {
                        return ['user_id' => $user->id];
                    })
                    ->create()
            )
            ->has(
                UserPhone::factory()
                    ->count(5)
                    ->state(function (array $attributes, User $user) {
                        return ['user_id' => $user->id];
                    })
                    ->create()
            )            
            ->create();
    }
}
Database\Seeders\UserSeeder::Database\Seeders\{closure}(): Argument #2 ($user) must be of type App\Models\User, null given
 /**
 * Add emails to this user account
 *
 * @return \Illuminate\Database\Eloquent\Factories\Factory
 */
public function emails(int $count)
{
    return $this->afterCreating(function (User $user) use ($count) {
        for ($i = 0; $i < $count; $i++) {
            $user->useremails()->create([
                    'email' => $this->faker->safeEmail,
            ]);
        }

    });
}
public function run()
{
    \App\Models\User::factory()
                ->count(5)
                ->emails(5)
                ->create();
}
public function run()
{
    \App\Models\User::factory()
                ->count(5)
                ->emails(5)
                ->phones(2)
                ->create();
}