When Laravel uses the $appends of the model modifier, faker cannot be used to generate fake data

version: laravel 5.7

when using the $appends attribute of the model modifier to add elements and using faker to generate false data, an error is reported:
Unknown column "sex_str"

Model related code:

$factory->define(\App\Models\User::class, function (Faker $faker) {
    return [

        "sex" => random_int(1, 2),
        
        ];
});

masking the $appends attribute is normal. How to solve the problem that model modifiers affect the generation of false data?

add:

The purpose of the

model modifier is that the sex of the data stock is of type int, and when taken out, the sex_str, is added to convert it to a string.

when generating fake data, sex is added to the faker.

Nov.17,2021

The

append array is usually a field that does not exist in the database. If you go to faker to generate simulation data, you must report an error Unknown column sex_str

.

-I am the dividing line-

after experimenting with Laravel5.7, there is no problem that the subject said, so the subject should check his own code carefully. There is no code to set up sex_str, to save this field when saving the database, so prompt Unknown column 'sex_str'.

class User extends Authenticatable
{
    use Notifiable;

    public static $sexAttr = ['', '', ''];

    protected $appends = ['sex_str'];

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

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

    public function getSexStrAttribute()
    {
        return array_key_exists($this->sex, self::$sexAttr) ? self::$sexAttr[$this->sex] : '';
    }
}
$factory->define(App\User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'sex' => random_int(1, 2),
        'email_verified_at' => now(),
        'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
        'remember_token' => str_random(10),
    ];
});
factory(App\User::class, 50)->create();

clipboard.png


@ iBrand
found the reason. The writing problem in seeder:

        $user = factory(\App\Models\User::class)->times(1000)->make();
        DB::table('users')->insert($user->toArray());

I use insert , I should use create

but using create will generate 1000 sql,insert will only generate one sql,
because some associated queries are nested in the factory, so the data generation is very slow.

change it to create first. I wonder if there is any other way?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b326c1-2be27.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b326c1-2be27.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?