Laravel is a web application framework with expressive, elegant syntax , MVC architecture , Blade template and Artisan CLi . Laravel is accessible, powerful, and provides tools required for large, robust applications.
Before creating your first Laravel project, you should ensure that your local machine has PHP and Composer installed.
- Install Lamp server
- Install composer
- Install laravel globally
composer create-project laravel/laravel <project name>
eg,
composer create-project laravel/laravel example-app
OR
composer global require laravel/installer
laravel new <project name>
eg,
composer global require laravel/installer
laravel new example-app
cd <project name>
eg,
cd example-app
php artisan serve
app/Console/kernal.php -- for hold custom artisan command
app/Exceptions/handler.php -- for hold custom handler (exception)
app/Http/Controllers/ -- Base Controller, API controller , website Controller etc
app/Http/Middleware -- access guard for routes of the website
app/Http/model -- models for all database table
app/Http/providers -- for external/ third party Service provider files
bootstrap -- for improve application performence (speed up) using cache
config -- store all confifuration files for the application (mail , auth , hash)
database/migration -- for creating new database table using laravel
database/factories & seeders -- generates fake data for testing purposes
public -- run the app using the index.php
routes -- web.php for website routes and api.php for api routes configs
resources -- frontend pages , css , js etc
storage -- location for stores files by users (eg , downloadable file)
test/unit -- for writing automation (unit) testing test cases
vendor -- for external packages (eg payment gateway , google login)
.env file -- for configuartion test and production configuration details
composer.json -- store all dependencies(packages) information
readme.md -- file for documentation
pakage system -- for sending mails and import excel and files data
get -- for view (hit url)
post -- store data
resource -- This method is for the routes of resource controller predefine functions
Route::<Method name>('path with respect to root', function(){
function code
});
eg;
Route::post('/2', function(){
echo "Fail";
});
Route::get('/beta/{name}/{id}',function($name,$id=null){
$data = compact('name','id');
return view('demo')->with($data);
});
in demo page;
enter {{name}} and {{id}} for displaying the data
Syntax -- Route::<Method name>('path with respect to root', [<ControllerName>::class,'<classname>']);
Route::get('/page7',[DemoController::class,'about']);
for front end files '.blade' is a template engine engine of Laravel. code for return a page (demo.blade.php) in custom route (/page1) is written bellow.
eg;
Route::get('/page1',function(){
return view('demo');
Route::<Method name>('path with respect to root', function(){
function code
});
API url-> 127.0.0.1:8000/api/<API name>
eg;
route::get('/get-data',function(){
return "Success";
});
API will be available in this url 127.0.0.1:8000/api/get-data
{{$variable_name}} -- for simple echo without html decode in blade
{{!!$variable_name!!}} -- for echo with html decode in blade
{{date(d-m-y)}} -- for echo date in blade
@if() @else @elseif -- for writing conditional in blade
@isset -- check weather the varible set
@unless -- unless the the condition is true ,do nothing
@for -- for loop
@while -- while loop
@php -- syntax for writing raw php
@continue -- for skip the current iteration and run the next
@break -- it will stop the loop
{{-- comment --}} -- syntax for write a comment
@include - for adding/include an another php file
@section -- it defines the section of a content which will be yeild
@yeild -- display the content of a given section
@extends -- it ineherit/take the layout the parameter file
eg, lets create a layouts folder with 3 files (footer,main,header) inside resources/views
main file code:
@include('layouts.header')
@yield('main-section')
@include('layouts.footer')
now use extends for including the main layout:
@extends('layouts.main')
the following datawill be merge with the view of the current page:
@section('main-section)
<data to be included>
@endsection
for dynamic title tages we can use @stack in header and @push in the current webpage
@stack('title') -- [in header.blade.php]
@push('title') <title> add the title here </title> @endpush -- [we can also add meta tags]
This are Class based php files.It works as a interpreter between view and model. we can group related request handling logic in controllers.
php artisan make:controller <cotroller name>
eg;
php artisan make:controller DemoController
php artisan make:controller <cotroller name>
eg;
php artisan make:controller SingleActionController --invokable
php artisan make:controller PhotoController --resource
syntax -- php artisan make:component <Component_name>
php artisan make:component Input
use <x-input/> tag for render the component in the view pages
php artisan config:cache (for first time env file setup)
php artisan migrate (for first time migration to create default tables)
php artisan make:migration create_table_name_table (for creating new table)
php artisan migrate --force
php artisan migrate:rollback (for undo the last migration)
php artisan migrate:refresh (for fresh run all migration)
syntax -- php artisan make:migration add_columns_to_<tablename>_table
php artisan make:migration add_columns_to_customers_table
This are Class based php files. Laravel include Eloquent ,an ORM to interact with database.Each table has a coresponding table to interact with the table
syntax -- php artisan make:model <model_name singular form>
eg,
php artisan make:model <model_name> --migration
syntax -- php artisan make:model <model_name in singular form> --migration
eg,
php artisan make:model Product --migration
create "helper.php" inside "app" folder
create functions which are cooomnonly use in development or production , so that you can call them anytime
add "files": ["app/helper.php"] to "composer.json"
composer dump-autoload
public function Set<field_name(in camelcase)>Attribute($value)
{
$this->attributes['<field_name(same as on the database )>']=ucwords($value);
}
eg, following function convert the cutomer_name filed from the customers table, as first character of each word to uppercase:
public function setCustomerNameAttribute($value)
{
$this->attributes['customer_name'] = ucwords($value);
}
public function getCustomerDobAttribute($value){
return date("d-M-Y",strtotime($value));
}
php artisan make:seeder CustomerSeeder
configure the "CustomerSeeder" file with rules
php artisan db:seed
use for loop for large number of entries