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

This is one of my Laravel posts.  I’m still learning Laravel so the posts are usually pretty basic and are more aimed at keeping track of things that I find useful so that I can come back to them again at a later date.  I hope you find them useful too.  If not, or if you think they are wrong then please leave a comment below.  Thank you.

Faster Laravel Hosting

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


You May Also Like…

Setting up SQLite for Laravel

Setting up SQLite for Laravel

Sometimes you may decide you want to use SQLite for your Laravel app instead of a full blown MySQL database. In that...