Creating Dummy Articles with the Same User using Laravel Eloquent

Written by SarahS

26th November 2019

If you have set up your Laravel installation with --auth, then you will already have a User class and Factory.

Then set up your Factory to create an article, it will look something like this:

$factory->define(Article::class, function (Faker $faker) {
    return [
        'user_id' => factory(\App\User::class),
        'title' => $faker->sentence,
        'excerpt' => $faker->sentence,
        'body' => $faker->paragraph


Fire up Tinker in the common line (Terminal) by running php artisan tinker.

To create, for example, 5 articles that will also create a new User for each one, run factory(App\Article::class,5)->create();

If you want to create 5 articles but assign them to a specific User, then run

If this post helped you or if you have any questions please leave me a comment below:


You May Also Like…