Laravel for Beginners - Learn Laravel 11 Basics in 1 Hour
Table of Contents
Introduction
This tutorial provides a comprehensive guide to getting started with Laravel 11, a popular PHP MVC framework. It covers the essentials of setting up a Laravel application and performing basic CRUD operations. Whether you are a beginner or looking to refresh your skills, this step-by-step guide will help you understand the core concepts of Laravel.
Step 1: Install Required Software
Before you can start with Laravel, ensure you have the following software installed:
- PHP: Download from Apache Friends
- Composer: Dependency manager for PHP, available at getcomposer.org
- Code Editor: Use Visual Studio Code for coding.
Step 2: Install Laravel
To create a new Laravel project, follow these commands in your terminal:
- Open your terminal.
- Navigate to the directory where you want to create your project.
- Run the following command:
composer create-project --prefer-dist laravel/laravel myapp
- Replace
myapp
with your desired project name.
Step 3: Run the Local Development Server
Once Laravel is installed, you can run the built-in web server:
- Navigate to your project directory:
cd myapp
- Start the web server:
php artisan serve
- Open your browser and go to
http://localhost:8000
to see your application.
Step 4: Understand MVC Architecture
Laravel follows the MVC (Model-View-Controller) architecture:
- Model: Represents the data and business logic.
- View: The user interface.
- Controller: Handles user input and interacts with models.
Step 5: Set Up Routing
Routing is essential for directing traffic within your application:
- Define routes in
routes/web.php
. - Example of a basic route:
Route::get('/hello', function () { return 'Hello, World!'; });
Step 6: Create Views
Views are created in the resources/views
directory. To return a view, modify your route:
Route::get('/hello', function () {
return view('hello');
});
Create hello.blade.php
in the views
directory with any HTML content.
Step 7: Use Blade Templates
Blade is Laravel's templating engine. Use Blade syntax for dynamic content:
<h1>{{ $title }}</h1>
Step 8: Generate Code with Artisan
Utilize Artisan to generate components easily:
- Create a controller:
php artisan make:controller MyController
- Generate a model:
php artisan make:model MyModel
Step 9: Configure Database Connection
Set up your database in the .env
file. Update the following lines with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Step 10: Run Migrations
Migrations are used for database schema management. Run migrations with:
php artisan migrate
Step 11: Create Models
Create a model to interact with the database:
php artisan make:model Post
Define the model in app/Models/Post.php
.
Step 12: Handle Form Requests
To handle form submissions:
- Create a form in your view.
- Define the route to handle the POST request.
- Use the model to save data.
Step 13: Implement Validation
Validate incoming requests to ensure data integrity:
$request->validate([
'title' => 'required|max:255',
'body' => 'required',
]);
Step 14: Use Route Model Binding
Simplify route handling by using model binding:
Route::get('/posts/{post}', [PostController::class, 'show']);
Step 15: Handle Deletion and Flash Messages
To delete a record, use:
$post->delete();
For flash messages, use:
session()->flash('message', 'Record deleted successfully.');
Step 16: Add Pagination
To paginate results:
$posts = Post::paginate(10);
Conclusion
In this tutorial, you learned how to set up a Laravel application, configure routing, create views, manage database interactions, and implement CRUD operations. As a next step, consider exploring Laravel's documentation for deeper insights into advanced features and best practices. Happy coding!