Laravel Tutorial
Laravel is a popular PHP framework designed for building robust, scalable, and maintainable web applications. It provides a simple and expressive syntax, tools for common web development tasks, and an ecosystem that streamlines application development.
1. Installing Laravelβ
Step 1: Installing Composerβ
Laravel uses Composer for dependency management. Install Composer from getcomposer.org.
Step 2: Creating a New Laravel Projectβ
composer create-project --prefer-dist laravel/laravel myapp
cd myapp
Alternatively, you can install Laravel globally:
composer global require laravel/installer
laravel new myapp
cd myapp
Step 3: Running the Development Serverβ
Use the built-in server to view your application:
php artisan serve
Access your application at http://localhost:8000.
2. Laravel Application Structureβ
A typical Laravel application structure includes:
- app: Contains core application code, including Models, Controllers, and Middleware.
- routes: Defines application routes.
- resources: Holds views (Blade templates) and front-end assets.
- config: Stores configuration files.
- database: Contains migrations, seeders, and database configurations.
3. Defining Routesβ
Laravel routes are defined in the routes/web.php file.
Exampleβ
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return 'About Laravel';
});
Route Parametersβ
Route::get('/user/{id}', function ($id) {
return 'User ' . $id;
});
4. Creating Controllersβ
Controllers handle the logic for your application's routes. Create a controller using the Artisan CLI:
php artisan make:controller UserController
Example: UserControllerβ
In app/Http/Controllers/UserController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index() {
return "Listing all users";
}
public function show($id) {
return "User ID: " . $id;
}
}
Register Controller Routesβ
Route::get('/users', [UserController::class, 'index']);
Route::get('/users/{id}', [UserController::class, 'show']);
5. Using Blade Templatesβ
Blade is Laravel's templating engine, used to create dynamic views.
Example Blade Templateβ
Create a Blade file at resources/views/welcome.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Laravel App</title>
</head>
<body>
<h1>Welcome to Laravel</h1>
<p>{{ $message }}</p>
</body>
</html>
Passing Data to Viewsβ
In your controller:
public function index() {
return view('welcome', ['message' => 'Hello, Laravel!']);
}
6. Working with Eloquent ORMβ
Eloquent is Laravel's ORM for interacting with the database.
Creating a Modelβ
php artisan make:model User
Defining Database Tablesβ
Eloquent maps models to database tables. To create a users table, define a migration:
php artisan make:migration create_users_table
In the migration file (database/migrations/*_create_users_table.php):
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
Run the migration:
php artisan migrate
7. Database Operations with Eloquentβ
Inserting Dataβ
$user = new User;
$user->name = 'Alice';
$user->email = 'alice@example.com';
$user->save();
Retrieving Dataβ
$users = User::all();
$user = User::find(1);
Updating Dataβ
$user = User::find(1);
$user->name = 'Alice Smith';
$user->save();
Deleting Dataβ
$user = User::find(1);
$user->delete();
8. Form Handling and Validationβ
Defining a Formβ
In resources/views/form.blade.php:
<form action="/submit" method="POST">
@csrf
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<button type="submit">Submit</button>
</form>
Handling the Form Requestβ
Define the route and controller method:
Route::post('/submit', [FormController::class, 'submit']);
public function submit(Request $request) {
$request->validate([
'name' => 'required|max:255',
]);
return "Form submitted successfully!";
}
9. Artisan CLIβ
Laravelβs command-line tool, Artisan, helps with various tasks.
Common Artisan Commandsβ
- Serve the application:
php artisan serve - Clear cache:
php artisan cache:clear - Create a model:
php artisan make:model ModelName - Run migrations:
php artisan migrate
10. Laravel Middlewareβ
Middleware filters HTTP requests in your application.
Creating Middlewareβ
php artisan make:middleware CheckAge
In app/Http/Middleware/CheckAge.php:
public function handle($request, Closure $next) {
if ($request->age < 18) {
return redirect('home');
}
return $next($request);
}
Applying Middleware to a Routeβ
Route::get('/profile', function () {
// Profile page
})->middleware('check.age');
Summaryβ
This tutorial covered Laravel basics:
- Installing Laravel and setting up a project.
- Creating routes and controllers to handle requests.
- Using Blade templates to render views.
- Working with Eloquent ORM for database operations.
- Handling forms and validating data.
- Using Artisan and middleware for additional functionalities.
Laravel is a powerful framework that simplifies building robust and feature-rich PHP applications.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.