Answers for "Laravel 8 Factory - One To Many (Polymorphic) Relationship"

PHP
0

Laravel 8 Factory - One To Many (Polymorphic) Relationship

<?php

namespace DatabaseFactories;

use AppModelsComment;
use AppModelsPost;
use AppModelsVideo;
use IlluminateDatabaseEloquentFactoriesFactory;

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 January-16-2022

Code answers related to "Laravel 8 Factory - One To Many (Polymorphic) Relationship"

Browse Popular Code Answers by Language