Laravel
Building web applications with Laravel: routing, Eloquent models, authentication, queues and deployment, explained with practical examples you can use in real projects.
No stories here just yet!
We have not written about this yet. Please explore another topic, or come back soon!
Questions about this topic
Quick, clear answers to what readers ask most about this topic.
What is Laravel?
Laravel is a free, open-source PHP framework for building web applications. Laravel gives developers ready parts for routing, databases, login, email, queues and testing, so they write less repeated code. The framework follows the model, view, controller pattern and has a large community.
What is Laravel used for?
Laravel is used to build web applications such as management systems, customer portals, online stores, booking platforms and APIs for mobile apps. Laravel suits any project that needs users, a database and business rules. Many companies run large private systems on Laravel because the framework is stable and easy to extend.
Is Laravel good for beginners?
Laravel is good for beginners who already know basic PHP, because its code is readable and its official documentation is clear. Laravel also has a large community, many tutorials and starter kits that set up login pages for you. Learning plain PHP first makes the framework much easier to understand.
How do I install Laravel on my computer?
You install Laravel by first installing PHP and Composer, then running composer create-project laravel/laravel followed by your project name. The Laravel installer, called with laravel new, does the same and offers starter kits. After that, php artisan serve starts a local server so you can open the app in your browser.
Which PHP version does Laravel need?
Laravel needs a recent PHP 8 version, and each Laravel release states the lowest PHP version it supports. Newer Laravel releases drop support for older PHP versions as those reach the end of their security support. Always check the upgrade guide of the Laravel version you plan to use.
What is routing in Laravel?
Routing in Laravel connects a web address and an HTTP method, such as GET or POST, to the code that answers it. Routes are written in files such as routes/web.php for pages and routes/api.php for APIs. A route can call a closure or a controller method and can carry middleware for login and other checks.
What is Eloquent in Laravel?
Eloquent is Laravel's ORM, the part that lets you work with database tables as PHP classes called models. Each model maps to a table, and each object maps to a row, so you can read and save data without writing SQL. Eloquent also handles relationships such as one-to-many and many-to-many.
How do Eloquent relationships work in Laravel?
Eloquent relationships in Laravel are methods on a model, such as hasMany, belongsTo and belongsToMany, that describe how two tables are linked. Once defined, you can read related data like $order->customer without writing a join. Eager loading with the with method loads related rows in one go and avoids slow repeated queries.
What is the N+1 query problem in Laravel?
The N+1 query problem in Laravel happens when code loads a list of records and then runs one extra query for each record to fetch related data. A page with 100 orders can end up running 101 queries and become slow. Eager loading with the with method fixes the problem by fetching the related rows in one query.
What are migrations in Laravel?
Migrations in Laravel are PHP files that create and change database tables in code. Running php artisan migrate applies them, so every developer and every server gets the same database structure. Because migrations live in version control, the history of every table change is kept.
What is Blade in Laravel?
Blade is Laravel's template engine for writing HTML pages with simple tags for loops, conditions and layouts. Blade escapes printed values by default, which protects pages from cross-site scripting. Templates are compiled to plain PHP and cached, so Blade adds almost no cost to each request.
How does authentication work in Laravel?
Authentication in Laravel works through guards that check who the user is and providers that load users from the database. Starter kits such as Laravel Breeze add ready login, registration and password reset pages. Passwords are hashed with bcrypt or Argon2, so they are never stored in plain text.
What is the difference between Laravel Sanctum and Laravel Passport?
Laravel Sanctum gives simple token and cookie login for single-page apps, mobile apps and basic APIs, while Laravel Passport is a full OAuth2 server. Most projects only need Sanctum because it is lighter and easier to set up. Passport fits cases where outside applications must log in on behalf of your users.
What are middleware in Laravel?
Middleware in Laravel are layers that check or change a request before it reaches your code or a response before it leaves. Common middleware handle login checks, CSRF protection, rate limiting and language settings. You can attach middleware to single routes, groups of routes or the whole application.
How do queues work in Laravel?
Queues in Laravel move slow tasks, such as sending email or creating reports, out of the web request and into background jobs. A job is pushed to a queue driver like Redis or the database, and a worker started with php artisan queue:work runs it. The user gets a fast response while the heavy work finishes in the background.
When should I use queues in a Laravel application?
You should use queues in a Laravel application whenever a task takes more than a moment or talks to an outside service, such as email, file processing or payment webhooks. Queues keep pages fast and let failed jobs retry on their own. Laravel Horizon gives a dashboard to watch Redis queues.
How do I schedule tasks in Laravel?
You schedule tasks in Laravel by defining them in the scheduler with methods such as daily or everyFiveMinutes, then running php artisan schedule:run every minute from one cron entry on the server. The scheduler then decides which tasks are due. This keeps all timed jobs in code instead of many separate cron lines.
How do I validate form data in Laravel?
You validate form data in Laravel with the validate method on the request or with a Form Request class that holds the rules. Rules such as required, email, max and unique check each field before your code uses it. When validation fails, Laravel sends the user back with the error messages and the old input.
How does Laravel protect against common web attacks?
Laravel protects against common web attacks with CSRF tokens on forms, escaped output in Blade, parameter binding in database queries and hashed passwords. These defaults block cross-site request forgery, cross-site scripting and SQL injection in normal use. Developers still need to validate input, check permissions and keep the framework updated.
How do I deploy a Laravel application to a server?
You deploy a Laravel application by pulling the code to the server, running composer install without dev packages, setting the .env file and running the migrations. Then you cache the config, routes and views, and point the web server to the public folder. Many teams automate these steps from Git so every release is the same.
What server does a Laravel application need?
A Laravel application needs a server with PHP, a web server such as Nginx or Apache, a database such as MySQL or PostgreSQL and Composer. Redis is often added for cache and queues, and a process manager keeps queue workers running. Hostrena, Linkysoft's hosting brand, offers web app hosting for Laravel deployed straight from Git.
How do I make a Laravel application faster?
You make a Laravel application faster by caching the config, routes and views, eager loading relationships and adding indexes to the database columns you search. Moving slow work to queues and caching heavy query results in Redis help further. Turning on PHP OPcache on the server gives a large gain for little effort.
How do I test a Laravel application?
You test a Laravel application with PHPUnit or Pest, which come set up in every new project. Feature tests send fake requests to your routes and check the response and the database, while unit tests check single classes. Running php artisan test runs the whole suite.
Can Laravel handle large applications with many users?
Laravel can handle large applications with many users when it runs on good servers with caching, queues, database indexes and more than one app server behind a load balancer. The framework itself is rarely the limit, slow queries and missing caches usually are. Linkysoft builds custom web applications with PHP, Laravel, MySQL and Redis for this reason.
How often should I upgrade my Laravel version?
You should upgrade your Laravel version at least before your current version stops getting security fixes. Laravel releases a major version about once a year, and each one comes with an upgrade guide. Small, regular upgrades are much easier than jumping several versions at once.