Laravel
Notes:
** XAMPP | PhpMyAdmin when using laptop office:
** http://localhost:8082
** http://localhost:8082/phpmyadmin/
** Git clone https://github.com/bradtraversy/lsapp
** Reference / Youtube Link: The Net Ninja
Introduction
What is Laravel?
● Framework for creating PHP Websites
● Works well with MySQL database
● Packed with useful features (blade, auth, eloquent)
MVC Pattern (Model, View, & Controller)
Figure 1.0
Installing Laravel
1. Install XAMPP
2. Install Composer (https://getcomposer.org/)
● Verify installation/version of Composer by typing composer -v on cmd
● To create file with Laravel, use the following commands:
○ composer global require laravel/installer
○ laravel new (title or file of project)
Figure 1.0
Figure 1.1
Routes & Views
● When a request to the server has been made, the request is handed off to a
route file and looks at the URL. The request made can either process some data
to save into the database, retrieve data, etc. which is injected to a View.
● View method is an HTML template
Figure 1.0
Input:
Figure 1.1
Output:
Figure 1.2
Passing Data to Views
● Setup different simple route handlers
● Get data from database or API endpoint and inject into the view
● Using of Blade template to output dynamic value
● Blade Template: {{ $variable }}
Input:
Figure 1.0
Figure 1.1
Output:
Figure 1.2
Input:
Figure 1.3
Figure 1.4
Output:
Figure 1.5
Blade Basics
● Use of Blade template engine to make it easier to work with data and logic inside
of regular PHP docs
● Blade template is compiled into regular HTML by Laravel
● @ symbol: directives used in Blade
Input:
Figure 1.0
Figure 1.1
Output:
Figure 1.2
Other Blade directives:
● @unless and @endunless
● @php and @endphp
● @auth and @endauth
Ex.:
Figure 1.3
Blade Loops
● Iteration arrays or lists of data and output template for each item in the array
Ex:
Figure 1.0 - For Loop
Figure 1.1
Figure 1.2
Figure 1.3 - For Loop
Figure 1.4
Figure 1.5 - For Each Loop
Figure 1.6
Figure 1.5 - Loop Index
Figure 1.6
Figure 1.7 - Embed If Statement in For Each Loop
Figure 1.8 - Embed If Statement in For Each Loop
Layout Files
● Generic layout file containing header, footer, etc. that can be used in different
views or parts of a website
● Create external layout file
● Use of directive such as @extends, @section and @endsection, @yield
Ex.:
exte
Figure 1.0 - Layout File
Figure 1.1 - Layout File with Yield Directive
CSS & Images
● Use external stylesheets and images
● JavaScript, CSS, and other frontend resources should be put into the public
folder
Query Parameters
● Queries are a way to inject values or variables into a URL
● Question mark (?) denotes the start of a query string
● Ampersand (&) denotes additional query parameters
● Query string can have several query parameters
● Detect query parameter and access the values of them
● Go to route handler
Ex.:
Figure 1.0 - Sample Query Parameters
Input:
Figure 1.1 - Return Request Query
Figure 1.2 - Output Request Query on HTML
Output:
Figure 1.3 - Output Request Query on Browser
Route Parameters
● Also known as Wildcards on Laravel
● Doesn’t have variable name
● Part of URL Structure
● Surround wild card name with curly braces to place different IDs
● Create ID to query database for a record and send the record into the View
● Pass ID into the view
Figure 1.0 - Route Parameters
Input:
Figure 1.1 - Pass ID into View
Figure 1.2 - Output ID on HTML
Output:
Figure 1.3 - Output ID on Browser
Controllers
● Laravel uses MVC (Mode, View, Controller) approach
● Create external file to register all callback functions for each set of routes which
is called the controller
● Controller - special class in Laravel where we define different route handler
functions
● Use one controller for a specific group of routes (ex. Use Pizza controller for
routers related to pizzas)
● Create a Controller file manually or through artisan to spin up a local
development server (ex. php artisan make:controller PizzaController)
● The controller file is placed under the folder hierarchy: app > Http > Controllers
● Controller naming convention uses CamelCase method
● When functions are defined on a Controller, it is called Actions
Figure 1.0 - How to Create Controller File using Artisan
Figure 1.1 - Use of Route with Controller
Figure 1.2 - Newly Created Controller File called PizzaController
Figure 1.3 - Sample Functions within the PizzaController
Connecting to MySQL
● Create new database using phpMyAdmin and using command line
● Add MySQL bin to Path in Environment Variable
Figure 1.0 - Create New Database on phpMyAdmin
Figure 1.1 - Add MySQL to Path