What is Laravel?
1. Laravel is an open source PHP Framework. It follows MVC design pattern.
2. It is robust and easy to understand.
3. Laravel has a very rich set of features which will boost the speed of web development.
4. Using Laravel we can develop a secure websites and prevents several web attacks.
Advantages
1. Laravel reuses the existing components of different frameworks which helps in creating a web application.
2. The web application becomes more scalable.
3. It includes namespaces and interfaces, thus helps to organize and manage resources.
4. It saves time while designing the web application.
5. Clean and simple routing.
Website : https://laravel.com/
MVC for Laravel
M - Model : Business Logic
V - View : Design + Layout
C - Controller : Bridge between Model & View
Image Courtesy : https://www.123rf.com
To understand MVC better please go through below link.
https://blog.pusher.com/laravel-mvc-use/
Basic Requirements for Laravel Version 5.7
PHP Version >= 7.1 , LAMP , MySql
To Install Latest PHP Version Type
$ sudo apt-get install php
To install laravel via the terminal you need Composer
1. First check whether the Composer is Installed or not by typing this command:
$ composer --version
2. If Installed output will be :
3. If not then use the following command to Install Composer :
$ curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
(it will take few minutes.)
4. To check if composer installed :
$ composer
Output wil be :
Laravel Package Installation
1. Go to your localhost directory by typing this command :
$ cd /var/www/
2. Now run below command (Project_Name As per your Project) to create laravel package (It will automatically create a directory named : Project_Name) :
$ composer create-project --prefer-dist laravel/laravel Project_Name
(NOTE : If there is directory permission error , use this command : $ chmod -R 775 /var/www/ )
3. Give permissions to your working directory by typing this command:
$ chmod -R 775 /var/www/Project_Name
4. Go to your Laravel project directory use this command :
$ cd /var/www/Project_Name
Now you are in your working directory (e.g. : /var/www/Project_Name$)
To Start Local Development Server
Use command below:
$ php artisan serve
Output will be :
Artisan : Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. All artisan commands for a Laravel project have to be run inside the root of the Laravel project.To get more information About Artisan :
https://laravel.com/docs/5.0/artisan
https://laravel.com/docs/5.7/artisan
(NOTE : After "$ php artisan serve" command you should not close the Terminal , because that’s your local server which has been created by you to run your project.
Go to http://127.0.0.1:8000 , you will see the Home page of Laravel.(If you are able to see that Homepage , then you have configured Laravel perfectly.)
For Database Connectivity
1. To go to your project directory : /var/www/Project_Name
2. Press Ctrl + h To unhide .env file.
To set working environment for laravel project you must have to set some environment variables for your project in this file.
Example:
APP_KEY=base64:fjfF1yuLyY5xsVVLIg1ArudaWY8UeSSvdE1DxrUvJ2M=
DB_DATABASE=DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=root
(NOTE : This APP_KEY is set automatically when you created your Project.)
The Entry Point of Any Laravel Project is: /var/www/Project_Name/public
If You Want to run Your project without using terminal, you can do that also:
Use this URL as reference : http://localhost/Project_Name/public/
The Directory Structure Of Laravel Package
Directory Location:
1. Models : MyProject/app/
Model is always related to database .
If we want to create a table, we have to make model file for it.
2. Views : MyProject/resources/views/
All views are saved in this directory.
The view file is always saved with “.blade.php” extension for laravel.
Views are User Interface files.
3. Controller : MyProject/app/http/controller/
All Controllers are stored in this directory.
The Controller always called first.
4. Route : MyProject/routes/web.php
In web.php set the routes for our project.
Example : Route::get('/about', 'PagesController@getAbout');
In above example "/about" is the page to redirect on.
PagesController is the controller class
@getAbout : The method in class PagesController and that method returns view for about Page.
URL : http://127.0.0.1:8000/about
Migration and Seeding
Using Migration and Seeding we can create our tables and insert data into it via terminal. There is some default Migration already given by the laravel.
To create a table(users) using migration :
1. Create migration for table
$ php artisan make:migration create_users_table
2. Migrate table into database
$ php artisan migrate
Create seeder for insert data into table
1. To create seeder
$ php artisan make:seeder UsersTableSeeder
2. To seed data into the table
$ php artisan db:seed
Example for Migration and Seeding
1. Create Migration(Articles) use the command below:
$ php artisan make:migration create_articles_table
after this command the file is created under location "database/migrations" named prefix_create_articles_table.php
In this file there is a up() method is given to create table.We have to add our table structure or schema in it to create table.
Example :
public function up(){
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('description');
$table->string('body');
});
}
2. Now migrate the table using command below
$ php artisan migrate
Table is migrated to the database(check in mysql server).
3. To create seeder use command below:
$ php artisan make:seeder ArticlesTableSeeder
The file is under location "database/seeds" , In this file there is a method named run() is given, add these lines in run() method to insert data.
Example :
public function run()
{
DB::table('articles')->insert([ 'title' => 'Cricket', 'description' => 'ICC', 'body' => 'India', ]);
}
4. There is file named DatabaseSeeder.php under "database/seed" which is given by laravel itself to execute our seeders. Every seeder class has to be called here.
Example :
public function run()
{
$this->call(ArticlesTableSeeder::class);
}
5. Now seed(insert) data into the table via the terminal using command below.
$ php artisan db:seed
To Make a Module (About Us), Follow These Steps
1. Create a controller named PagesController using this command :
$ php artisan make:controller PagesController
When you open this file there is class named PagesController which is already created.
2. Now we have to create a method named getAbout() to return view in this controller:
public getAbout()
{
return view(‘about’);
}
3. Now create a view named about.blade.php in view folder.
We can not create view using php artisan command, so we have to create it manually.
4. Add below piece of code In web.php file which is under routes folder to set route of the About Us page.
Route::get('/about', 'PagesController@getAbout');
5. You can easily access your module by using below path or URL.
http://localhost/Project_Name/public/about OR
http://127.0.0.1:8000/about
References :
Create Table Manually Using PhpMyAdmin
Follow the link to perform simple CRUD(Create-Retrieve-Update-Delete) operation using Laravel.
https://www.youtube.com/watch?v=XmXj0sXQRx4
To Get More Information About Laravel
https://laracasts.com
https://laravel-news.com
Create Basic Website Using Laravel
https://www.youtube.com/watch?v=jnvu1GpylP0
Create Simple Login Application Using Laravel
https://www.youtube.com/watch?v=OUFmwAnFclo