What would I do if I had to learn Laravel again after years of not using it?

Last updated Mar 29, 2024 Published Mar 20, 2024

The content here is under the Attribution 4.0 International (CC BY 4.0) license

For a long time, I used Laravel to build web apps and APIs for my professional career. I spent at least 4-5 years writing PHP apps with Laravel, which led to a book and later hosting two conferences in Brazil — one in 2016 and the other in 2017. Today, I would like to share what I would focus on if I had to relearn Laravel from scratch, and this post is an exercise that I am doing with that in mind. Please be aware that it’s been a while since I touched Laravel, so any input that might improve the content is welcome.

Introduction

Laravel is a full-stack framework with a multitude of functionalities and is a popular choice in the industry. However, deciding where to start and how to use it can be challenging. Choosing between different technologies can significantly impact how developers solve problems for businesses. The focus here is on Laravel, PHP, HTML, and database access knowledge. You don’t need to be an expert; just familiarity with these topics is fine. Whenever something doesn’t click, take a step back and analyze it from a distance to spot any insights needed to proceed.

This content is organized into starting with basics, a one-minute intro, routing, templating, data access, collection, and testing. Each section includes suggested exercises for practice, and each builds on top of the other—they serve as building blocks.

It is recommended to follow this content in sequence.

Starting with the basics

In my opinion, the first entry point should be to get to know your framework before diving into anything known as “best practice,” or as I prefer to call them, sensible defaults. Laravel offers a plethora of tools for building applications, including routing, templating, database access, string utilities, collection utilities, testing utilities, and more.

But before delving into Laravel itself, I would recommend understanding the basics of web applications (HTML, CSS, JavaScript), MVC architecture, and any interaction with a database, with or without an ORM. Keep in mind that if you’ve never worked with an ORM, it’s fine; Laravel provides a welcoming environment to understand its ORM, Eloquent.

Requirements

Make sure to have an environment where you can execute PHP applications through a web server, whether with or without Docker. Feel comfortable dealing with both the environment and the programming language. Identifying whether errors come from PHP or Laravel is key to understanding this content. Laravel also offers options for environments; if you choose that route, ensure you become familiar with it.

One-minute intro to Laravel

  • Installing https://laravel.com/docs
  • Go through a structured walkthrough with the folders
  • Share here how to map a get request to a controller -> emphasis in the controller
  • Intro to Artisan CLI

Exercises before jumping

  • Start a Laravel project from scratch
  • Within the project walk the project through and see the different files, and inspect the ones that are configuration-wise
  • Can Artisan help in the code generation?

Now that we already have an overview, let’s get back to Laravel and see step by step how to start with it.

Routing

PHP offers different ways of accessing routes that originate from the browser, as well as supporting various HTTP verbs. However, PHP and its functionalities are variable, thus leading frameworks like Laravel to offer utilities on top of that.

In that sense, routing should be one of the first features to be explored in Laravel. Laravel concentrates route declaration in a single place, in a file where all the routes are declared, and the framework is in charge of delegating the incoming requests to the corresponding controller.

The following is an example of creating a route in Laravel to respond to a GET request. Usually, GET and POST are the most frequently used HTTP verbs in web applications; however, sometimes PUT, PATCH, and DELETE are also needed to provide support for APIs.

Exercises before jumping

  • Create a new route /this/is/new/route that responds to the string “My content”
  • Within the project walk the project through and see the different files, and inspect the ones that are configuration-wise

Templating

Laravel also comes with its template engine, which aids in the quick building of web apps, and its name is Blade. Blade itself has many features to facilitate the rendering of PHP code into the web page. For developers accustomed to writing HTML, this is a walk in the park, as Blade adds the dynamics of PHP into the mix.

Here is the first connection with the MVC pattern; usually, the response from the controller delegates the data so the view can render the proper data.

Exercises before jumping

  • Do you relate Blade with other template engines?
  • Create a new route that points to /my/first-template and returns and renders a template named first_view.template.blade.php that should show “From template.blade.php”

Database access

Accessing data in any application is at the core; without data, an application is like a living thing missing a key aspect. Laravel offers an abstraction to deal with different databases. PHP itself offers utilities to access data from various database vendors. It is indeed a valuable learning exercise to understand how it works behind the scenes.

Eloquent

With Eloquent, the ORM from Laravel, more utilities are added on top of it, such as entity validations, automatic mapping from tables to objects, many-to-one, many-to-many, one-to-many mapping, and last but not least, collections.

Exercises before jumping

  • Create a table called laravel_after_years with the fields id (numeric, auto-increment and primary key) and description (text) and map it to a model Named LaravelAfteryears
  • Insert random data into the table manually
  • Create a page that displays the data inserted in the table

Migrations

With modern software development, new techniques have been introduced in the development lifecycle flow. One of them is migrations. Migrations are scripts that are versioned in the repository, tracking the changes made in the database. Laravel offers a migration mechanism that is embedded and transparent to developers. This is one of the key features of the framework, as it does not depend on any other third-party to offer a seamless experience.

Migrations are at the heart of database migrations, allowing developers to introduce changes in a reproducible manner.

Exercises before jumping

  • Use a migration to create the table laravel_after_years
  • Depict the migration execution flow

Collection utilities

Collections are an abstract concept that Laravel uses to create its own implementation. Five years ago, when I discovered the power of collections, it inspired me to share an overview of its features.

Collections help Laravel navigate the dynamics of the code and offer developers functionalities that assist in day-to-day activities. For example, it offers methods such as count, sum, and avg to work with the number of items, the total, and the average. Other functions such as map, filter, and flat mapping are also available.

Exercises before jumping

  • Compare how the collection map function works with the map function that PHP offers.
  • What type of data do Laravel models return in relationships that are many-to-one or many-to-many?
  • What type of data does Laravel return when the findAll method is used?

Exercises before jumping

Extra challenges

  • Solve the kata https://www.codurance.com/katas/smart-fridge with Laravel as setup.
  • Solve the kata https://www.codurance.com/katas/corporate-hotel-booking with Laravel

Advanced topics

From now on, you already have a basic understanding of the framework and its functionality. If you took the extra challenge, you even mastered the basics. In this section, the idea is to dive into other subjects that are often not used for simple web applications, such as cache, read/write databases, providers and specific configurations.

Providers

Laravel providers are part of the core of the framework. Providers are used to fulfill a series of common actions before features are available for developers to interact with, some common usages of providers are:

  • Register a new service
  • Bootstrap configurations when the framework starts
  • Extend the framework functionality

Exercises before jumping

  • Inspect the providers that come with the framework, the CachingProvider for example.

Packages

Packages are another form to share code across Laravel projects, it allow independent parties to provide packages and use them integrated in the application. The Laravel ecosystem is full of packages shared by the community.

Exercises before jumping

  • Create a package that integrates with the Open AI

Caching

Laravel offers different levels of caching throughout the application, one of the most used ones is the HTTP caching.

Exercises before jumping

  • Implement caching for a GET request that expires in 10 minutes

Resources