Home > Back-end >  laravel Database Faker relationship
laravel Database Faker relationship

Time:05-10

Im trying to create fake data base for testing for my two tables Endpoint and Request , here is my Endpoint factory

class EndpointFactory extends Factory
{
    protected $model = Endpoint::class;

    public function definition()
    {
        return [
            'url' => $this->faker->unique()->url,
        ];
    }
}

** this is my request table factory **

class RequestFactory extends Factory
{
    /** @var string  */
    protected $model = Request::class;

    public function definition() : array
    {
        return [
            'status'      => $this->faker->randomElement([200, 201, 401, 403, 422]),
            'endpoint_id' => 1,
            'user_id'     => $this->faker->unique()->numberBetween(1, 100),
            'method'      => $this->faker->randomElement(['POST', 'GET', 'DELETE', 'PUT']),
            'duration'    => $this->faker->numberBetween(1, 10),
            'result'      => json_encode(['id' => 1, 'name' => 'name']),
        ];
    }
}

** here my models for the two tables **

class Request extends Model
{
    use HasFactory;

    /**
     * @var string
     */
    protected $table = 'requests';

    protected static function newFactory(): RequestFactory
    {
        return RequestFactory::new();
    }

    public function endpoint(): BelongsTo
    {
        return $this->belongsTo(Endpoint::class);
    }
}
class Endpoint extends Model
{
    use HasFactory;

    /**
     * @var string[] 
     */
    protected $fillable = ['url'];

    /**
     * @var string 
     */
    protected $table = 'endpoints';

    protected static function newFactory(): EndpointFactory
    {
        return EndpointFactory::new();
    }

    public function requests(): HasMany
    {
        return $this->hasMany(Request::class);
    }
}

** the problem with testing , i want the relationship too for exemple **

 public function test_api_structure_for_single_Endpoint()
    {
   
      $Endpoint = Endpoint::factory()
      ->has(Request::factory()->count(5))->create();
}

** but i get only Endpoint table , the request table wont be created like in the laravel docs **

**ps im using laravel 9 **

CodePudding user response:

Factories just create database table entries, not actual tables. If your entire table is missing you need to create a migration.

if it's just the entries that aren't being added, you can use the ->each() chain function to iterate over each created endpoint, and save requests against them:

$Endpoint = Endpoint::factory()->create()->each( function($e) {
  $e->saveMany(Request::factory()->count(5)->make());
});

CodePudding user response:

If you want to make dummy data to model and its relations, remove the foreign column. Your Test file remains the same

return [
    'status'      => $this->faker->randomElement([200, 201, 401, 403, 422]),
    // 'endpoint_id' => 1,
   'user_id'     => $this->faker->unique()->numberBetween(1, 100),
   'method'      => $this->faker->randomElement(['POST', 'GET', 'DELETE', 'PUT']),
   'duration'    => $this->faker->numberBetween(1, 10),
   'result'      => json_encode(['id' => 1, 'name' => 'name']),
];
  • Related