The document is a comprehensive introduction to Laravel development, detailing its history, features, and installation process. It covers the MVC architecture, basic authentication, routing, CRUD operations, role implementation, file storage, middleware, and the Eloquent ORM and query builder. Additionally, it provides insights into creating a Laravel project while utilizing various tools and references for further learning.
Introduction to the presentation on Laravel Development by Muhammad Mahdi Hasan, a developer with a B.Sc. in Computer Science & Engineering.
The presentation covers Laravel's history, installation, MVC architecture, authentication, CRUD operations, roles, middleware, ORM, Query Builder, and file storage.
Laravel is a free PHP web framework created by Taylor Otwell, following the MVC pattern, with notable features such as built-in authentication.
Key benefits of Laravel include MVC support, security features, object-oriented libraries, database migration, and great community support.
Installing Laravel requires server configuration, Composer, Laravel installation methods (installer or create-project), and setting up configuration files.
MVC architecture separates application logic (Model), UI (View), and control flow (Controller), facilitating scalable web applications.
Basic authentication in Laravel can be set up using Laravel/ui package, scaffolding, and navigating to the registration route after migration.
Laravel routing differentiates between web (web.php) and API (api.php) routes, illustrating how to define web routes for user interactions.
Overview of Laravel project structure including app, bootstrap, config, database, public, resources, storage, tests, and vendor directories.
Utilizing Blade template for consistent layouts and performing CRUD operations using Artisan commands for creating models, migrations, and controllers.
Implementing user roles for permission control, defining authorization through Gates, and utilizing Middleware for request filtering and authentication.
Eloquent ORM provides a simple Active Record implementation, allowing data interactions for corresponding database tables and facilitating queries.
Integrating a shopping cart in Laravel involves using Composer, adding products to the cart, showing cart content, and manipulating cart items.
A list of useful Laravel resources, including official documentation, tutorials, and additional reading materials for further learning.
Introduction
Muhammad Mahdi Hasan
B.Sc. in Computer Science & Engineering
from International University of Business
Agriculture & Technology
Currently working as a laravel developer in
Creative Software Ltd.
I. Laravel History,Environment Setup & Laravel
Installation
II. MVC architecture, Basic Authentication & Routing
III. Laravel Template Mastering & CRUD Operation
IV. Laravel Role Implementation, File storage &
Middleware
V. Eloquent ORM & Query Builder, Cart, Session &
Others
Content
5.
Implement laravelproject & database connection
Know how MCV architecture pattern works
Know how to create, read, update & delete data
from MySql database with laravel project
Basic authentication & role implementation
Laravel project upload online demo
Our Goals
Laravel is afree, open-source PHP web
framework, created by Taylor Otwell and
intended for the development of web
applications following the model–view–
controller (MVC) architectural pattern.
What Is Laravel ?
8.
Developer(s) Taylor Otwell
Initialrelease June 2011; 8 years ago
[1]
Stable release 6.9.0
[2]
/ 2019-12-19[±]
Written in PHP
Type Web framework
License MIT License
Website laravel.com
• The source code of Laravel is hosted on
GitHub and licensed under the terms of MIT
License.
9.
History
Taylor Otwell createdLaravel as an attempt to
provide a more advanced alternative to the
CodeIgniter framework, which did not provide certain
features such as built-in support for user
authentication and authorization. Laravel's first beta
release was made available on June 9, 2011,
followed by the Laravel 1 release later in the same
month.
Installing Composer
Laravel utilizesComposer to manage its
dependencies. So, before using Laravel,
make sure you have Composer installed
on your machine.
16.
Installing Laravel
There Are2 ways to install laravel project:
• Via Laravel Installer
• Via Composer Create-Project
17.
• Via LaravelInstaller
First, download the Laravel installer using Composer
than :
composer global require laravel/installer
laravel new blog
• Via Composer Create-Project
Alternatively, you may also install Laravel by issuing
the Composer create-project command in your
terminal:
composer create-project --prefer-dist laravel/laravel blog
• Note
Above commands will install the latest versions of laravel so if
you want to specify a version, you can use composer:
composer create-project laravel/laravel=5.8 myapp
The Model-View-Controller (MVC)is an
architectural pattern that separates an
application into three main logical
components: the model, the view, and the
controller. Each of these components are
built to handle specific development aspects
of an application. MVC is one of the most
frequently used industry-standard web
development framework to create scalable
and extensible projects.
25.
Basic Authentication
Install thelaravel/ui Composer package and run
php artisan ui vue --auth in a fresh Laravel
application. After migrating your database, navigate
your browser to http://your-app.test/register or any
other URL that is assigned to your application.
These commands will take care of scaffolding your
entire authentication system!
26.
In laravel, thereare 2 routes file web.php and api.php.
web.php file is used for registering all the web
routes like -
mywebsite.com/about
or
mywebsite.com/contact
api.php is used for registering all the routes related
to an api. We are only using web routes so don’t
worry about any api routes.
Routing
app −This directorycontains the core code of the
application.
bootstrap −This directory contains the application
bootstrapping script.
config −This directory contains configuration files of
application.
database −This folder contains your database migration and
seeds.
public −This is the application’s document root. It starts the
Laravel application. It also contains the assets of the
application like JavaScript, CSS, Images, etc.
30.
resources −This directorycontains raw assets such as the
LESS & Sass files, localization and language files, and
Templates that are rendered as HTML.
storage −This directory contains App storage, like file
uploads etc. Framework storage (cache), and
application-generated logs.
test −This directory contains various test cases.
vendor −This directory contains composer dependencies.
Template Mastering
The BladeMaster Template is where we can place all the
boilerplate that all pages will typically make use of. Most times
you can name this file something like master.blade.php. All
view files that you would like to have make use of your master
page can now use the @extends keyword to do so. Since our
master page has the name of master.blade.php, in our view
files we will use @extends('master'). You can name the
master page something else if you want to, you’ll just need to
make sure to extend the other name. For example if your
master page is default.blade.php, you can use
@extends(‘default‘) in your view files.
In many webprojects, we have different user roles
interacting with the system. Each role has its own
permission. Every feature of the system can be enabled
or disabled for these roles. We can define users
permissions in our codes and check if they are
authorized to do the requested action or not. A better
way, mostly in more flexible systems, is to create a role
and authorization management system. I’ll explain how
to implement a Laravel authorization system and define
users permission based on their roles.
Implementing User Roles into a Laravel Application. User
Roles allow us to control access to a page or feature within
an application.
Role Implementation
44.
Gate:
Gates are Closuresthat determine if a user is authorized to
perform a given action and are typically defined in the App
Providers AuthServiceProvider class using the Gate facade.
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
$gate->define('isAdmin', function($user)
{
return $user->user_type == '0';
});
$gate->define('isCompany', function($user)
{
return $user->user_type == '3';
});
$gate->define('isUser', function($user)
{
return $user->user_type == '1';
});
}
Middleware provide aconvenient mechanism for
filtering HTTP requests entering your application.
For example, Laravel includes a middleware that
verifies the user of your application is authenticated.
If the user is not authenticated, the middleware will
redirect the user to the login screen. However, if the
user is authenticated, the middleware will allow the
request to proceed further into the application.
Middleware
The Eloquent ORMincluded with Laravel
provides a beautiful, simple Active Record
implementation for working with your
database. Each database table has a
corresponding "Model" which is used to
interact with that table. Models allow you to
query for data in your tables, as well as
insert new records into the table.
Eloquent ORM
51.
Laravel's database querybuilder provides a
convenient, fluent interface to creating and
running database queries. It can be used to
perform most database operations in your
application and works on all supported
database systems.
Query Builder
Laravel Shopping cart
Runthe Composer require command from the Terminal:
composer require gloudemans/shoppingcart
GloudemansShoppingcartShoppingcartServiceProvider::class
'Cart' => GloudemansShoppingcartFacadesCart::class,
Check & and optionally add a new line to the aliases array:
Check & add a new line to the providers array:
Gloudemans Shoppingcart is a simple shoppingcart
implementation for Laravel.
54.
public function addtocart(Request$request)
{
$products=Product::find($request->id);
Cart::add([
'id'=>$request->id,
'qty'=>$request->qty,
'name'=>$products->productname,
'price'=>$products->price,
'options' =>
[ 'image' => $products->image ]
]);
return back()->withInput();
}
public function cartshow()
{ $cartProduct = Cart::content();
$cartCount = Cart::count();
return view(‘ViewPage',compact(‘cartProduct’,‘cartCount’);
}
55.
public function update(Request$request)
{
Cart::update($request->rowId, $request->qty);
return redirect()->back();
}
public function delete($id)
{
Cart::remove($id);
return redirect()->back();
}
Cart::destroy();
Cart::total();
Cart::subtotal();
Cart::count();
56.
Session
Since HTTP drivenapplications are stateless, sessions provide a way to
store information about the user across multiple requests. Laravel ships
with a variety of session backends that are accessed through an
expressive, unified API. Support for popular backends such as
Memcached, Redis, and databases is included out of the box.
The session configuration file is stored at config/session.php. Be sure to
review the options available to you in this file.
@if(session(‘message'))
<div class="alert alert-dismissible alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ session(‘message') }}</strong>
</div>
@endif