Search
 
SCRIPT & CODE EXAMPLE
 

PHP

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,
        ]);
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: on keyup jquery for search php 
Php :: php to html 
Php :: IlluminateValidationRulesRequiredIf 
Php :: vendor folder command for custom errors laravel 
Php :: Laravel Mix npm run production error 
Php :: php post http 
Php :: php send to message to mobile number using springedge 
Php :: how to upload images to sql database php PDO 
Php :: laravel array validation 
Php :: lowercase in array php 
Php :: find largest element of an array in php 
Php :: php mysql row to json 
Php :: update php version cpanel 
Php :: simple php round example 
Php :: last insert id in laravel 
Php :: how to create php message 1 
Php :: How to protect your website from DDos Attack? 
Php :: php invoke method 
Php :: change php version 
Java :: No Java files found that extend CordovaActivity. 
Java :: android get screen width and height 
Java :: jsp import class 
Java :: how to upgrade java 8 to 11 in ubuntu 
Java :: java iterate through hashmap 
Java :: change port in spring boot 
Java :: love2d set screen size 
Java :: keyset sort java 
Java :: How to change progressbar color xml android 
Java :: set fontcolor of jframe java 
Java :: android glide dependencies 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =