• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How To Seed A Database In Laravel 4

General · September 1, 2014

I have been writing a lot about Laravel lately and recently I was asked about database table seeding. Chances are you already use the awesome migrations feature, but probably don’t use database seeding. I have seen people using migrations to seed a table, but it’s messy and not exactly ideal. Using the seeding functionality in Laravel, you’ll discover how easy it actually is.

When should I use seeding?

I personally use seeding in Laravel quite extensively during the development phase of a web application. Being able to add in mock user accounts, pages and other data for testing purposes is a lot easier than manually doing it every time.

Another use that I find myself using is seeding roles and permissions. In my web applications, I will generally have a basic set of permissions that every Laravel site will get (admin, guest and basic user roles) as well as some basic permissions.

A precursor

Where to put your seed files?

All custom seeding classes go into app/database/seeds a new installation of Laravel 4 will have a file called DatabaseSeeder which is where we will call our custom created seed class(es) to fill our database with content.

What do I name them?

The name of your custom seeding class does not matter. However, it is advisable to name your seeding classes in camelCase. If you are wanting to seed a roles table in your application, you would call it something like RolesTableSeeder or SeedRolesTable

In action

Now we’ve explained the basics, when to use seeding and how it can help us, lets get to the meat of the post.

Rather than explain step-by-step, I’ll show an example of using seeding instead.

The custom role seeding file app/database/seeds/RoleTableSeeder.php

Calling our newly created seeding file

In the file app/database/seeds/DatabaseSeeder.php you will notice a run function. Within this function beneath the Eloquent::unguard function call, we will call our seeding file.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • I Joined Truth Social Using a VPN and Editing Some HTML to Bypass the Phone Verification
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How To Mock uuid In Jest
  • How To Decompile And Compile Android APK's On A Mac Using Apktool
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Wild Natural Deodorant Review

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz