Today I brought you another tutorial on Laravel. We have to connect to a database when we do a project with the manipulation with data. So, this is the way how to connect to the database and make tables using migrations available in Laravel. Previously created project will be used here also, for my ease. If you didn't start a project, visit here to start a new project in Laravel.
Create an application key for your project. It will be used to configure sessions later. See the below image to get the idea about this key published in Laravel official site.
Step 1
Create a new database called laravel_test in your http://localhost/phpmyadmin.
Open your project folder and find a file called .env(not .env.example). You can see a lot of configurations. Among them, find the term DB_CONNECTION. Then make changes like this. I changed only DB_HOST, DB_DATABASE, DB_USERNAME and DB_PASSWORD. My database is laravel test. I have not set a password and the default user; root is given. If you have changed these when configure PHP, provide them accordingly.
Open your project folder and find a file called .env(not .env.example). You can see a lot of configurations. Among them, find the term DB_CONNECTION. Then make changes like this. I changed only DB_HOST, DB_DATABASE, DB_USERNAME and DB_PASSWORD. My database is laravel test. I have not set a password and the default user; root is given. If you have changed these when configure PHP, provide them accordingly.
Step 2
Open the file called database.php included in config folder. You will see several configurations should be used according to the SQL version used in a project. Since I use MYSQL, I only configure the part reserved for MYSQL. Use the same credentials used in the previous step.
Step 3
Go into the folder called migrations included in database folder. It has a migration file made by default. Delete that file. We are going to make new changes to the database. So don't worry about it.
Step 4
Now we have to make a model for database functionalities. I think you will know how MVC architecture works. Model is responsible to handle database as you know. To make a model, we use cmd. Open cmd and go into the project folder. Then type the below command to make a model. My model name is User_model.
php artisan make:model User_model -m
In Providers folder now you have the model names as User_model.
Step 5
This is the most important step. Unlike Codeigniter, this Laravel framework can change the database using migrations. Open the migration file in database/migrations folder. This is made by default when the model is created.
Now I'm going to change this migration file to create a database table according to the way I needed. In the migration file, up function creates database table and down function removes the table. I will change up function. My database table name is users.
$table->increments('id')
- This will add a column called id with auto increment.$table->timestamps
- Adds two columns called created_at and modified_at.Now I need another two columns for first name and last name of users. How to do it?
Step 6
Now we are ready to migrate. But there's an additional step to be followed. If you migrtae now, there will be an error. To avoid it you have to do something. In your project folder, open the file called AppServiceProvider. File path: Http/Providers/AppServiceProvider.php
Now edit the file and update it with 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 7
All changes have been saved I made. Now I need to create this users table in localhost/phpmyadmin. So, Laravel gives us a migrate command to do this easily! Type this command in cmd. Then new migrations will be created and updated.
php artisan migrate
Now you have come to the last part of this article. The database table has been created. Go to localhost/phpmyadmin and select the database laravel_test. You willl see two tables called users and migrations.
I think now you can understand the way to create a database connection and manage tables in it. These migrations are just one amazing feature in Laravel. So, try to get used to this framework. Still, I'm also learning this...
Good Luck!
Laravel Database Connection and tables
Reviewed by DAL
on
February 21, 2018
Rating:
No comments: