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(),
];
}
@faisalfayazkhan I get a Class 'Database\Factories\UsersFactory' not found
error, after running Users::factory()->count(20)->create()
.
You have a typo in your tinker command. Its User::factory
and not Users::factory
User::factory()->count(20)->create()
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community