How to populate databases in Laravel
Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders
directory. By default, a DatabaseSeeder
class is defined for you. From this class, you may use the call
method to run other seed classes, allowing you to control the seeding order.
To generate a seeder, execute the make:seeder
Artisan command. All seeders generated by the framework will be placed in the database/seeders
directory:
php artisan make:seeder UserSeeder
A seeder class only contains one method by default: run
. This method is called when the db:seed
Artisan command is executed. Within the run
method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.
namespace Database\Seeders;
use App\Models\Link;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LinkSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//only import seeds if DB is empty.
if (!Link::count()) {
// do your imports here
// you can use model factories or query builders too
}
}
}
Running seeders
You may execute the db:seed
Artisan command to seed your database. By default, the db:seed
command runs the Database\Seeders\DatabaseSeeder
class, which may in turn invoke other seed classes. However, you may use the --class
option to specify a specific seeder class to run individually:
php artisan db:seed
php artisan db:seed --class=UserSeeder
-
How to Get the Query Executed in Laravel 5
By default, the query log is disabled in Laravel 5: Don’t worry, you can enable the query log by running the following: // enable query log DB::enableQueryLog(); Then you can display the log like t...
Questions -
What is the difference between Factory and Seeder in Laravel?
Both Factory and Seeder are used to generate testing data for your application. But there are some differences: Factory By using factories you can easily create test data for your Laravel applicati...
Questions -
What is the difference between collection and query builder in Laravel?
Query Builder Builder is a layer of abstraction between your application and the database. Typically it's used to provide a common API for you to build platform-agnostic database queries. $users = ...
Questions -
How to log data in JSON in Laravel
Logging data in JSON format in Laravel can be done using the built-in Monolog logging library that Laravel uses under the hood. Here are the steps to configure Laravel to log data in JSON format: O...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github