
The ultimate Laravel tutorial for agencies
By John | 27 Jan 25
Got new starters, trainees or team members from other areas of the agency who want to learn Laravel? Good! You’re in the right place! This ultimate guide to the Laravel framework is designed to help your team grow in their Laravel development and start offering more to your clients.
Before you hand us over to your team, if you need hosting for your Laravel projects, Nimbus is here to help. Our powerful and secure hosting environment is perfect for websites and applications built using Laravel. To book a tour of our Laravel hosting platform or find out more, get in touch! Right, let’s get to learning, shall we?
Introduction to Laravel.
What is Laravel?
Laravel is one of the most popular PHP frameworks today. It’s elegant, versatile, and packed with features that make development faster and more enjoyable. Laravel is a PHP framework built to ease the development of web applications. It provides built-in features like routing, database management, and authentication, allowing developers (you) to focus on building the application rather than reinventing the wheel.
You’ve been given this guide by your agency to help you gain a better understanding of the Laravel framework. In this guide, you’ll be given step-by-step instructions, learn best practices and key Laravel concepts. It will give you a good understanding of Laravel and even help you build your first web application. However, support from other Laravel developers within your company will still be needed to master the art of coding in Laravel.
Why use Laravel?
If you’ve never used Laravel before, you might be wondering why it’s popular. After all, there are plenty of other PHP frameworks out there – some that you’re likely already familiar with. Developers gravitate towards Laravel for many reasons, but the key ones are:
- Simplicity – Laravel offers a clean and simple syntax, making development faster.
- Ecosystem – Laravel comes with tools like Laravel Forge, Laravel Vapor, and Envoyer for easy deployment, management, and scaling.
- Community – Laravel has a large community of developers and plenty of resources for ongoing support.
Larvel has one of the strongest and most helpful communities in development and the simplicity of using it is a thing of beauty. What’s even better, though, is if you have a background in development already, learning Laravel may only take you a month or two!
Getting to know the Laravel ecosystem.
Now you’re a bit more familiar with Laravel, it’s time to meet the ecosystem that makes it the powerhouse of development that it is. The Laravel development environment consists of:
- Eloquent ORM – An elegant ORM (Object-Relational Mapping) that makes database queries easy and readable.
- Blade Templating Engine – A simple but powerful templating engine with zero overhead.
- Artisan CLI – A command-line interface for automating common tasks.
- Vite – A tool for compiling CSS and JavaScript.
Before we jump into using these tools for the first time, we need to set up Laravel.
Setting up Laravel.
System requirements.
Before installing Laravel, make sure your system meets the following requirements:
- PHP – Version 8.0 or greater
- Web Server – Apache or Nginx
- Database – MySQL, PostgreSQL, SQLite, or SQL Server
- Composer – Dependency manager for PHP
Installing composer.
Laravel uses Composer to manage its dependencies. To install Composer:
- Visit Composer and download the installer.
- Follow the installation instructions for your operating system.
Installing Laravel.
Once Composer is installed, you can install Laravel globally by running:
Now you can create a new Laravel project with:
Alternatively, you can use Composer directly to install Laravel:
Setting up a development environment.
To set up a local PHP environment, we’d recommend using Laravel Valet (for macOS) or Laravel Homestead (for a virtualised environment). For Windows users, consider Laragon or XAMPP to configure your web server and database.
The other developers at your agency will likely have a preferred environment to work in too, so ask around and get ready for an interesting few hours of debating which is best!
How to do database setup.
Laravel uses environment variables to store database credentials – which you’ll need to set up next.
Open the .env file and configure the following values:
Run the following command to migrate the default Laravel tables to the database:
Congrats, you’ve just run your first Laravel command line! Let’s dive deeper into the PHP file now and learn some more, shall we?
Learning the Laravel basics.
Laravel directory structure.
Without understanding the structure of a Laravel project, you’ll find it difficult to navigate and build your first Laravel website or application. Here’s a breakdown of the key folders:
- app/ – Contains the core logic of your application (models, controllers, services).
- routes/ – Defines the routes for the application.
- resources/views/ – Contains Blade view files (HTML templates).
- config/ – All configuration files for the project.
- database/ – Migrations, seeders, and factories are stored here.
Routing basics.
In Laravel, routes are defined in the “routes/web.php” file. Here’s an example:
To handle form submissions:
Controllers.
Controllers handle logic and direct requests to the appropriate resources. Create a controller using Artisan:
In “UserController.php”:
Views (Blade Templates).
Laravel uses Blade for templating. Blade allows you to write plain HTML along with some Blade-specific syntax. For example:
Blade also provides conditionals and loops:
Models and Eloquent ORM.
Laravel uses Eloquent to interact with the database. Each model represents a table in the database. For example, the `User` model interacts with the `users` table. You can create a model with:
In your model, you can define relationships, fillable fields, and more:
Database and migrations.
Database configuration.
Ensure your .env file is configured with the correct database settings.
Introduction to migrations.
Migrations allow you to version control your database schema. Create a migration using Artisan:
Within the migration file, define the schema:
Run migrations using:
Database seeding.
You can use seeders to populate your database with test data. Create a seeder with:
Within the seeder:
Run the seeders:
Handling forms and validation.
Handling forms in Laravel.
Forms in Laravel are handled using standard HTML or Blade syntax with some Laravel-specific enhancements. For example, you can use Blade’s `csrf_field()` helper to include a CSRF token in your form for security:
Laravel automatically includes CSRF protection for all POST requests to prevent cross-site request forgery.
Form validation.
Laravel provides a simple way to validate incoming requests. You can either do this directly in the controller or by using a form request class.
Here’s an example of handling validation in a controller:
Alternatively, create a custom request validation class:
In the “StoreUserRequest” class:
Displaying validation errors.
If validation fails, Laravel automatically redirects the user back to the previous page and flashes the validation errors to the session. You can display these errors in the Blade view like this:
Old input and redirecting.
Laravel automatically flashes old input to the session when validation fails, so you can easily display the old input:
In the controller, you can redirect back with input like this:
Authentication and authorisation.
Introduction to Laravel Breeze.
Laravel Breeze provides a simple implementation of authentication for your Laravel applications, including registration, login, and password reset functionality. To install Laravel Breeze:
Breeze scaffolds the basic authentication views and controllers for you. It’s ideal for a quick setup for small projects. Anyone would say it makes it a breeze to set things up!
Customising authentication logic.
If you need more advanced features like social login or multi-factor authentication, Laravel offers Jetstream to make things quick and easy. You can also customise the `LoginController` and “RegisterController” provided by Breeze.
For example, to modify the registration process, update the “RegisterController”:
Authorisation: roles and permissions.
Laravel has a built-in authorisation system based on gates and policies. Gates are simple, closure-based permissions, while policies provide more structured authorisation logic.
Defining a gate:
In Blade, you can authorise actions with:
Middleware for authorisation.
Middleware is another way to handle authorisation in Laravel. It can be used to restrict access to certain routes based on roles, for example:
Apply this middleware in the route:
Queues and background jobs.
Introduction to queues.
Queues allow you to defer the processing of time-consuming tasks, like sending emails or generating reports, to the background. Laravel supports several queue backends, such as Beanstalk, Redis, Amazon SQS and more.
Configuring queue drivers.
You can configure your queue driver in the .env file. For example, to use Redis as a queue driver:
Defining jobs and dispatching them.
Now you can create a new job using Artisan:
In the “SendWelcomeEmail” job, define the logic for processing, for example:
You can then dispatch the job from a controller:
Running and monitoring queue workers.
You can run your queue worker using:
If you’re creating production environments, you can use Supervisor to keep the workers running continuously.
Testing in Laravel.
Introduction to testing.
Laravel includes PHPUnit for testing. So, if you’re already familiar with other PHP environments, testing in Laravel might come naturally to you. Laravel tests are stored in the `tests/` directory, and they are divided into unit tests and feature tests. Unit tests test individual pieces of code, while feature tests focus on the behaviour of the entire system. Here’s how to set up both types of tests.
Writing unit tests.
A basic unit test example:
Writing feature tests.
Feature tests allow you to test how different parts of your application interact. For example, testing a login route:
Testing HTTP requests and responses.
Laravel provides plenty of useful methods for testing HTTP requests and responses. For instance, you can test whether a route returns the correct view using:
Test-driven development (TDD) with Laravel.
TDD is a development approach where you write tests before writing the actual code. Laravel’s testing tools make it easier to practice TDD. The idea is to first write a failing test, implement the code to pass the test and then refactor the code while keeping the test green.
For example, in a TDD approach to developing a feature you might:
- Write a failing test for a new feature
- Write the minimal code required to pass the test
- Refactor the code while keeping the test green
Here’s an example:
This feature is really handy when you’re learning Laravel as it helps keep you on the right track while allowing you to test new lines of codes and features.
What's next?
Well, you’ve just completed our ultimate guide to Laravel – nicely done! Ready to get stuck in? Don’t forget, there are plenty of resources available for people learning Larvel. From forums to YouTube videos. And, don’t forget to ask your teammates. They’ll be an amazing help in the following months as you begin to master Laravel.
Oh, and don’t forget to tell your boss about Nimbus Hosting – the no. 1 platform for Laravel hosting in the UK. No matter how many apps or websites they need to host, we can handle it!