Brief Summary
This YouTube video is a comprehensive Laravel for Beginners course, extracted from a premium course. It covers fundamental Laravel concepts, project setup, routing, controllers, views, database interactions with Eloquent ORM, and deployment considerations. The course aims to equip beginners with the skills to build and deploy Laravel applications.
- Covers fundamental Laravel concepts for beginners.
- Includes project setup, routing, controllers, and views.
- Focuses on database interactions using Eloquent ORM.
- Discusses deployment strategies and best practices.
- Offers coupon codes within the video for free access to premium content.
Introduction
The author introduces a Laravel for Beginners course, comprising 37 modules and over 24 hours of content, including videos, written lessons, quizzes, and deployment guides. The first 19 modules are combined into a single, free YouTube video. The author encourages viewers to like, subscribe, and comment, noting the video includes time codes for easy navigation. The course is designed to be beginner-friendly, avoiding extra JS or CSS frameworks, and will be updated based on viewer feedback. The author also announces a new free Laravel React e-commerce website course and offers coupon codes within the video for free access to all courses on his website.
Prerequisites
The course is suitable for individuals with or without prior Laravel experience, but a solid grasp of PHP, object-oriented programming, HTML, CSS, and basic SQL database knowledge is recommended. Familiarity with tables, columns, primary and foreign keys, data types, queries, joins, and basic insert, update, and delete operations is beneficial.
Project Full Demo
The course project is a car selling website, grabacar.xyz, deployed in a production environment. The homepage features a hero slider with options for buyers and sellers, a search form, and the latest listed cars. Users can search by make, model, state, city, type, year, and price range. Car details pages include multiple images, detailed descriptions, and seller information. Potential buyers can view a partially visible phone number and click a button to reveal the full number via an Ajax request. Registered users can sign up or log in using email and password or social accounts like Google or Facebook. Authenticated users can add cars to their watch list, manage their profile details, and list their own cars for sale.
The Importance of Deployment
The author emphasizes the importance of deploying projects to a production environment to showcase them in a portfolio and provide a demo link. Custom domain deployment enhances professionalism and demonstrates the ability to take a project from start to finish. The course uses GitHub Actions for project deployment, and the author recommends Hostinger for hosting, citing its advantages such as domain purchasing, shared hosting, VPS hosting, cloud hosting, DNS settings management, and DDoS protection. A VPS server with a pre-installed Laravel application is used, and a special discount coupon code "learnlaravel" is provided for Hostinger.
Getting Started with Laravel
Laravel is introduced as a popular PHP framework known for its clean syntax and tools that simplify web application development. Key benefits include quick setup, security features like XSS and SQL injection protection, high performance, a flexible routing system, and the Eloquent ORM for database interactions. Laravel follows the MVC architecture, offers built-in testing support, authentication and authorization, caching, error handling, and the Blade templating engine. It also supports task scheduling, a robust queue system, multilingual support, and easy maintenance and updates.
Routing
The request lifecycle in Laravel starts with public/index.php
, which loads the autoloader and creates the application. Service providers configure the application, and the router matches the request to a function, potentially executing middleware. The controller sends a response, possibly rendering a view. Configuration options are primarily in the .env
file, with additional options in the config
folder. To create a new route, the Route
facade is used, specifying the HTTP method (e.g., get
), URL, and associated function.
Controllers
A controller is a class associated with one or more routes, responsible for handling requests. Controllers group similar routes together, such as a ProductController
for product-related logic. Controllers can be created manually or using the PHP artisan make:controller
command. To associate a route with a controller action, an array is used specifying the controller class and method name. Multiple methods can be grouped under a single controller using Route::controller
. Single action controllers, generated with the --invokable
flag, have a single __invoke
method that is executed when the controller is called. Resource controllers provide a convenient way to handle typical CRUD operations, with methods like index
, create
, store
, show
, edit
, update
, and destroy
.
Views - The Basics
Views are files responsible for presentation logic, stored under the resources/views
folder, typically in Blade format. Blade is a templating engine that mixes HTML with PHP using a clean syntax, supporting template inheritance, directives, and reusable components. Views can be created manually or using the PHP artisan make:view
command. To render a view, the view
function is used, specifying the view name, which can include subfolders using dot notation.
Views - Displaying Data
Data can be passed to views using an associative array as a second argument to the view
function or using the with
method. Variables are output in Blade files using double curly braces {{ $variable }}
. Global shared data can be declared in a service provider using the View
facade's share
method, making it available in all Blade files. Functions and classes can also be used within Blade files to output data. Variables are escaped by default to prevent XSS, but unescaped output can be achieved using double exclamation marks {{!! $variable !!}}
. To prevent Blade from processing certain expressions, the @
symbol is used, and the @verbatim
directive can be used to ignore entire sections.
Views - Blade Directives
Blade directives are special keywords prefixed with @
used in Laravel's templating engine to simplify common tasks. Directives are converted into plain PHP code, enhancing readability and maintainability. Common directives include @if
, @for
, @foreach
, @extends
, and @include
. Comments in Blade are defined using {{-- comment --}}
, which are not visible in the page source. Conditional directives like @if
, @elseif
, and @else
are used for conditional rendering. The @unless
directive is the opposite of @if
. Other directives include @isset
, @empty
, @auth
, @guest
, and @switch
. Loop directives include @for
, @foreach
, @forelse
, and @while
. Continue and break directives can be used within loops.
Website Layout with Template Inheritance
Template inheritance in Blade allows defining a base layout and extending it in other views. The @extends
directive specifies the layout to inherit, and @section
and @yield
are used to define and output content sections. The @show
directive defines a section and immediately outputs its content. The @parent
directive includes the content of the parent section. The @hasSection
directive checks if a section is defined. The @checked
, @disabled
, @readonly
, @required
, and @selected
directives conditionally add HTML attributes.
Components
Components are reusable pieces of user interface that encapsulate HTML markup, styles, and logic. Laravel supports class-based and anonymous components. Anonymous components are Blade files located under a specific folder inside resources/views
. They are included using HTML tags starting with x-
. Components can be nested in subfolders, with the folder structure reflected in the tag name (e.g., x-admin.card
). Components can use slots to insert custom content, with default slots using the $slot
variable and named slots using <x-slot>
tags. Class-based components have a corresponding PHP class that defines the component's logic and data.
Create Basic Pages
The video transitions to building the car selling website, starting with creating a HomeController
and associating it with the homepage route. The welcome Blade file is deleted, and a hardcoded "index" text is returned from the index
method of the HomeController
.
Introduction to Databases
Laravel supports various databases, including MariaDB, MySQL, PostgreSQL, SQLite, and SQL Server. Database configurations are located in config/database.php
, with the default connection name taken from the .env
file. The .env
file specifies database credentials, and the database driver is set to SQLite by default.
Migrations
Migrations are used to manage and version control the database schema, allowing for creating, modifying, and sharing database changes. Migration files are located under database/migrations
and consist of a timestamp followed by a descriptive name. Each migration file contains an anonymous class extending the Migration
class, with up
and down
methods to apply and revert changes, respectively.
Eloquent ORM Basics
Eloquent ORM (Object-Relational Mapping) allows interacting with databases using PHP objects rather than raw SQL queries. Each database table has a corresponding model, located under app/Models
, which extends the Illuminate\Database\Eloquent\Model
class. Models provide methods for CRUD operations and support relationships between tables.
Eloquent ORM Relationships
Eloquent ORM relationships define how different database tables are related, including one-to-one, one-to-many, and many-to-many relationships. Relationships are defined as methods in the model classes using methods like hasOne
, hasMany
, belongsTo
, and belongsToMany
.
Factories
Factories generate fake data for models, aiding in testing and seeding. Factories are located in the database/factories
directory and extend the Illuminate\Database\Eloquent\Factories\Factory
class. The definition
method returns an associative array of attributes and their fake values.
Data Seeding
Seeders populate the database with sample and test data, defined in the database/seeders
directory. Seeders are run using the PHP artisan db:seed
command and can call other seeders. The run
method in a seeder class defines the data insertion logic.
Output Data on the Website from the Database
The video demonstrates how to retrieve data from the database and display it on the website. The Homecontroller
is updated to select cars from the database and pass them to the index
view. The index
view is then modified to iterate over the cars and display their properties using Blade syntax.
Database Where Clause
The video covers various where
clauses in Laravel's query builder, including multiple where
clauses, orWhere
, whereNot
, whereAny
, whereAll
, whereBetween
, whereNotBetween
, whereNull
, whereNotNull
, whereIn
, whereNotIn
, whereDate
, whereMonth
, whereDay
, whereYear
, whereTime
, and whereColumn
.
Data Pagination
Pagination is implemented using the paginate
method on the query builder, which automatically handles setting the limit and offset. The links
method is used to render pagination links in the view. The default pagination view can be customized by publishing the vendor pagination views and modifying the Tailwind CSS template.
Requests & Responses
The video discusses how to access request data and return responses in Laravel. Request data can be accessed by accepting a Request
instance in the controller method or using the request()
global function. Various methods are available on the Request
object to access URL, method, headers, and other request information. Responses can be returned using the response()
helper, allowing for setting status codes, headers, and content types. Redirects can be implemented using the redirect()
helper, specifying the URL or route name to redirect to.
Outro
The video concludes with a thank you message and encouragement to explore the complete course on the author's website.