PHP code example of thedoctor0 / laravel-factory-generator

1. Go to this page and download the library: Download thedoctor0/laravel-factory-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thedoctor0 / laravel-factory-generator example snippets


Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('username');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->integer('company_id');
    $table->rememberToken();
    $table->timestamps();
});

class User extends Model {
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
}



declare(strict_types=1);

namespace Database\Factories;

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

/**
 * @extends Factory<\App\Models\User>
 */
final class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            'name' => faker()->name,
            'username' => faker()->userName,
            'email' => faker()->safeEmail,
            'password' => bcrypt(faker()->password),
            'company_id' => \App\Company::factory(),
            'remember_token' => Str::random(10),
        ];
    }
}
bash
php artisan generate:factory
bash
php artisan generate:factory User Company
bash
php artisan generate:factory --force
bash
php artisan generate:factory --dir app/Models
bash
php artisan generate:factory --recursive
bash
php artisan vendor:publish --tag="factory-generator"