php artisan make:factory CommentFactory --model=Comment
Feb 13, 2021
9
Level 21
Laravel 8 Factory - One To Many (Polymorphic) Relationship
Given the following database table structure, what would go in the CommentFactory definition function?
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
All I've got is a mess!
Level 28
Here is your factory:
<?php
namespace Database\Factories;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Video;
use Illuminate\Database\Eloquent\Factories\Factory;
class CommentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Comment::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$commentable = $this->commentable();
return [
'body' => $this->faker->paragraph,
'commentable_id' => $commentable::factory(),
'commentable_type' => $commentable,
];
}
public function commentable()
{
return $this->faker->randomElement([
Post::class,
Video::class,
]);
}
}
and when you want to control the commentable you can refer to:
https://laravel.com/docs/8.x/database-testing#polymorphic-relationships
or:
$video = Video::factory()->hasComments(3)->create();
$comments = Comment::factory()->count(3)->for(
Video::factory(), 'commentable'
)->create();
tweak it to fit your needs, hope this helps you
13 likes
Please or to participate in this conversation.