PHP code example of makeabledk / laravel-factory-enhanced

1. Go to this page and download the library: Download makeabledk/laravel-factory-enhanced 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/ */

    

makeabledk / laravel-factory-enhanced example snippets


$team = factory(Team::class)->create();
$users = factory(User::class)->times(2)->create();
foreach ($users as $user) {
    factory(TeamMember::class)->create([
        'team_id' => $team,
        'user_id' => $user,
        'role' => 'admin'
    ]);
}

$team = Team::factory()
    ->hasAttached(
        User::factory()->count(2),
        ['active' => true]
    )
    ->create();


$team = Team::factory()
    ->with(2, 'users', ['pivot.role' => 'admin'])
    ->create();



namespace App\Models;

use Makeable\LaravelFactory\HasFactory;

class User 
{
    use HasFactory;
    
    // ...
}



namespace Database\Factories;

use Makeable\LaravelFactory\Factory;

class UserFactory extends Factory
{
    public function definition()
    {
        return [
            // ...
        ];
    }
}

Server::factory()->with(3, 'sites')->create();

Server::factory()->online()->with(3, 'sites')->create([
    'name' => 'production-1'
]);

Server::factory()
    ->with('team')
    ->with(3, 'sites')
    ->create();

Team::factory()
    ->with(3, 'servers.sites')
    ->create();

Team::factory()    
    ->with(2, 'servers')
    ->with(3, 'servers.sites')
    ->create();

Team::factory()    
    ->with(2, 'online', 'servers')
    ->create();

Team::factory()    
    ->with(2, 'online', 'servers')
    ->andWith(1, 'offline', 'servers')
    ->create();

Team::factory()    
    ->with(2, 'online', 'servers')
    ->andWith(1, 'offline', 'servers')
    ->with(3, 'servers.sites')
    ->create();

factory(Team::class)    
    ->with(2, 'servers', ['name' => 'laravel.com'])
    ->create();

Team::factory()    
    ->with(2, 'users', ['pivot.role' => 'admin'])
    ->create();

Server::factory()
    ->apply(2, 'online', ['name' => 'laravel.com'])
    ->with(3, 'mysql', 'databases')
    ->create();

Server::factory(2, 'online', ['name' => 'laravel.com'])
    ->with(3, 'mysql', 'databases')
    ->create();

Team::factory()    
    ->with('servers', fn (ServerFactory $servers) => $servers
        ->count(2)
        ->active()
        ->with(3, 'sites')
    )
    ->create();

factory(2, Server::class)->with(1, 'sites')->create();