Answers for "How to make a custom factory in Laravel"

PHP
0

How to make a custom factory in Laravel

<?php

namespace Database\Factories;

use App\Models\Comment;
use App\Models\Post;
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()
    {
        $post_ids = Post::pluck('id')->toArray();

        return [
            'comment' => $this->faker->text(),
            'post_id' => $post_ids[array_rand($post_ids)]
        ];
    }
}
Posted by: Guest on July-18-2021
0

laravel factory

php artisan tinkerProduct::factory()->count(500)->create()
Posted by: Guest on June-13-2021
0

add factory data laravel

For Laravel version 7.* and less
factory(App\User::class, 3)->make();

Use create method to persist them to the database:
factory(App\User::class, 3)->create();
Posted by: Guest on November-28-2020

Code answers related to "How to make a custom factory in Laravel"

Browse Popular Code Answers by Language