Support the ongoing development of Laravel.io →
Artisan Blade Authorization
Last updated by @ajax30 1 year ago.
0
Solution

The problem is with the syntax. I have copied the example from the official laravel 8.x documentation below.

use App\Models\User;
 
/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    return [
        'user_id' => User::factory(),
        'title' => $this->faker->title(),
        'content' => $this->faker->paragraph(),
    ];
}

Link to the example

0
Solution selected by @ajax30

@faisalfayazkhan I get a Class 'Database\Factories\UsersFactory' not found error, after running Users::factory()->count(20)->create().

0

You have a typo in your tinker command. Its User::factory and not Users::factory

User::factory()->count(20)->create()
0

The working example of PostFactory is below that I executed on my machine using the tinker command App\Models\Post::factory()->count(10)->create() and i was able to generate 10 posts with users as well.

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;


class PostFactory extends Factory
{

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
             'user_id' => User::factory(),
            'title' => $this->faker->title(),
            'description' => $this->faker->paragraph(),        ];
    }
}

And make sure you have UserFactory.php file present in the folder Database\Factories that is included by default in the Laravel 8 application.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.