Answers for "Laravel polimorfic faker"

0

Laravel polimorfic faker

<?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,
        ]);
    }
}
Posted by: Guest on May-26-2021
0

Laravel polimorfic faker

$video = Video::factory()->hasComments(3)->create();
Posted by: Guest on May-26-2021
0

Laravel polimorfic faker

$comments = Comment::factory()->count(3)->for(
    Video::factory(), 'commentable'
)->create();
Posted by: Guest on May-26-2021

Browse Popular Code Answers by Language