Laravel Guide
Laravel Guide
1. What is Laravel?
Laravel is a robust and elegant PHP framework designed for web application
development. It provides a clean and beautiful syntax and includes many features
out of the box, such as routing, authentication, and an ORM (Eloquent).
2. Setting Up Laravel
Prerequisites
PHP >= 7.3
Composer (PHP dependency manager)
A web server like Apache or Nginx
Installation
Install Laravel via Composer:
bash
Copier le code
composer global require laravel/installer
Create a New Laravel Project:
bash
Copier le code
laravel new my-laravel-app
Alternatively, you can use Composer directly:
bash
Copier le code
composer create-project --prefer-dist laravel/laravel my-laravel-app
Navigate to the Project Directory:
bash
Copier le code
cd my-laravel-app
Run the Development Server:
bash
Copier le code
php artisan serve
Your application will be accessible at http://localhost:8000.
3. Core Components
Routing
Routes define how your application responds to various requests. They are defined
in the routes/web.php file.
Example Route:
php
Copier le code
Route::get('/', function () {
return view('welcome');
});
Controllers
Controllers handle the logic for your application. They are stored in the
app/Http/Controllers directory.
Creating a Controller:
bash
Copier le code
php artisan make:controller MyController
Example Controller Method:
php
Copier le code
public function index() {
return view('welcome');
}
Models and Eloquent ORM
Models represent your database tables. Laravel’s Eloquent ORM provides an active
record implementation to interact with your database.
Creating a Model:
bash
Copier le code
php artisan make:model MyModel
Example Model:
php
Copier le code
class MyModel extends Model {
protected $table = 'my_table';
}
Migrations
Migrations are used to create and modify database tables. They are stored in the
database/migrations directory.
Creating a Migration:
bash
Copier le code
php artisan make:migration create_my_table
Example Migration:
php
Copier le code
public function up() {
Schema::create('my_table', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
Views
Views are located in the resources/views directory and are used to display HTML
content. Laravel uses the Blade templating engine for views.
blade
Copier le code
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<h1>{{ $title }}</h1>
</body>
</html>
Passing Data to Views:
php
Copier le code
Route::get('/', function () {
return view('welcome', ['title' => 'Welcome to My App']);
});
4. Building a Simple Application
Create a Model and Migration:
bash
Copier le code
php artisan make:model Product -m
Update the migration file to add fields for your products.
Run Migrations:
bash
Copier le code
php artisan migrate
Create a Controller:
bash
Copier le code
php artisan make:controller ProductController
Define Routes:
In routes/web.php:
php
Copier le code
Route::resource('products', ProductController::class);
Add CRUD Methods to Controller:
In app/Http/Controllers/ProductController.php:
php
Copier le code
public function index() {
$products = Product::all();
return view('products.index', compact('products'));
}
Create Views:
In resources/views/products/index.blade.php:
blade
Copier le code
@foreach($products as $product)
<p>{{ $product->name }}</p>
@endforeach
5. Conclusion
Laravel provides a rich set of tools and features for developing web applications.
By understanding its core components—routing, controllers, models, migrations, and
views—you can build powerful and maintainable applications.