Wednesday, December 26, 2018

Create simple CRUD using Laravel 5.7 (Part 1 - Create Database Table)


Hey guys...Welcome back. Today we are going to create a CRUD (Create, Read, Update, Delete) using Laravel 5.7.

In this tutorial as a first step we are going to setup the database to store our data. First open the xampp server and start Apache & MySQL server. Open web browser and call ‘localhost/phpmyadmin’ in address bar. Then create a new database using any name. I created database named ‘institute’.

OK. Now we have to do configuration in Laravel project file. In your Laravel project folder there is a file named .env. Open that file.  In that file there are three things ‘DB-DATABASE’, ‘DB_UASERNAME’, ‘DB_PASSWORD’. We need to change this. Change the DB-DATABASE into institute (or what ever you put as your DB name). Then change DB_UASERNAME into root (most commonly the username is ‘root’ and if you have change privileges change according to that). Normally the DB_PASSWORD is null for root. (If you have it put it).

DB_DATABASE=institute
DB_USERNAME=root
DB_PASSWORD=null

OK. Now we need a table to store the data. Yep... In larval, there is command call ‘migration’ to deal with the database and tables. We use this to create a table.
Open command prompt. Go to the project directory.

cd\
cd\xampp\htdocs\MyFirstPro\blogs

Now fired up the code ‘php artisan make:migration create_students_table  --create=students’. Then create the table migration file. You can see it in database->migrations folder inside the project folder.

Note: When you named the table use plural words as the table name. There is a concept that using in Laravel.

Now we need to create the table. Before that we need to add the table fields to the migration file. I add two fields name(string) and age(integer) that different data types. 






































OK.. Then using  the command prompt (in project directory) we can create the table using the command ‘php artisan migrate’.

Note: If there is error in migration you need to set the default leangth of string. For that you need to edit the ' app->Providers->AppServiceProvider.php ' file. Import Schema file to it. For it add  ' use Illuminate\Support\Facades\Schema;line on the top of the page. Then edit the boot funtion with adding the code lineSchema::defaultStringLength(191); ' . Then type ' php artisan:migrate fresh '.
 
OK.. now our table is ready. You can see it php myadmin in xampp. In next  tutorial we will discuss about how to create the controller and the model in Laravel. Hope this is this is help to you. Also hope your  comments. Thank you. 
 


No comments:

Post a Comment

Create simple CRUD using Laravel 5.7 (Part 4 - Modify Controller, Model and Create route)

Hey guys...Welcome back. Today we are going to create controller and modal file in Laravel. In the controller file, you can see after we g...