banner image
Sedang Dalam Perbaikan

Database Seeding with Laravel


Hello guys! Today I came with another Laravel tutorial. I think now you know how to handle your data stored in a database. I have done a plenty of Laravel articles now. if you missed my articles, here you can get them.
Post category - Laravel

I'm going to introduce a simple and easy way to upload data into your database without using phpmyadmin. This is another special feature available in Laravel. The process called as Database Seeding. What's this? Let's move on..

As usual create a laravel project first.

composer create-project laravel/laravel LaravelSeed

Then create a new database to store our data. In my case I name it as laravel_seed. Go to phpmyadmin and create the database. 
Now I will explain step by step...Open your project in a text editor..

Step 1 - Database Configuration

Configuration 1 - .env file

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_seed
DB_USERNAME=root
DB_PASSWORD=

Configuration 2 - config/database.php
       'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel_seed'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]

Configuration 3 - avoid migration error

Open app/Providers/AppServiceProvider.php file and replace this code.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}


Step 2 - Prepare migrations


Configuration 1 - Delete unwanted migration files

Laravel create two migration file by default. Delete the migrations for password resets and users. Create a new model called Post. We are going to store some records of posts.

Configuration 2 - Create a model

We need to create a new model for posts. Create this with migrations.. Use a dingular name for model. Then the migration will be created in plural form. Here, posts will be the table.
php artisan make:model Post -m

Configuration 3 - Re-arrange posts migration

Replace this code in users migration file.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('description')->unique();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}

Configuration 4 - Migrate the file for the table - posts

Now we are ready to migrate. Open a cmd and go into the project folder. Then run this command.
php artisan migrate

What is the output? Two DB tables will be there in your database, named as migrations and posts. select the posts table. Currently it's empty.


Step 3 - Make a Database Seeder


In the project folder, there's a folder called seeds within the database folder. There's a file called DatabaseSeeder.php. This is the file that does our job. To proceed, we need a seeder file to connect with the function in DatabaseSeeder. In your cmd, type this command.
php artisan make:seeder PostSeeder

Now there will a be file with the name; PostSeeder within the seeds folder. We can include the records row by row. There's a format for this. For each column, we need to provide data. But for auto increment columns and timestamps columns don't need data as they are added automatically. Place the below code in PostSeeder.

<?php

use Illuminate\Database\Seeder;
use App\Post;

class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Post::create([
'title' => 'Post 1',
'description' => 'This is post one'
]);

Post::create([
'title' => 'Post 2',
'description' => 'This is post two'
]);

Post::create([
'title' => 'Post 3',
'description' => 'This is post three'
]);
}
}

Then we need to run this seeder to upload the data into our database. So, open the file called, DatabaseSeeder and replace the code with this. In the function run(), we have to call the seeder file.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(PostSeeder::class);
}
}

Now we have done the work! Go to phpmyadmin and check your table, there should be three records in the table.


OR you can check using cmd also.. Go to xampp/mysql/bin folder and copy tha path. Paste in cmd. Now you are in the mysql bin folder. Then type mysql -u root and enter to the command place for mysql..Look at this..


I think now you know how to upload data to a database without using phpmyadmin. Laravel is a great PHP framework I found on the earth which comes with awesome features. So I invite you to be similar with this. You web developing life will be marvelous in PHP side.

Good Bye!




Database Seeding with Laravel Database Seeding with Laravel Reviewed by DAL on April 08, 2018 Rating: 5

No comments:

Powered by Blogger.