Laravel is a powerful and popular PHP framework used to build web applications. It provides a flexible and streamlined development experience with a variety of tools and features to help develop robust web applications quickly and efficiently.
In this tutorial, we will go through the process of building a simple web application using Laravel. We will cover the basics of Laravel, including installation, configuration, routing, and views, as well as more advanced topics such as database migrations and authentication.
Prerequisites
Before we begin, make sure you have the following prerequisites installed on your system:
- PHP 7.1 or higher
- Composer
- MySQL
Step 1: Installing Laravel
To install Laravel, open up your terminal and run the following command:
composer global require laravel/installer
This will install the Laravel installer globally on your system, allowing you to create new Laravel projects with ease.
Step 2: Creating a New Laravel Project
To create a new Laravel project, run the following command:
laravel new project-name
Replace “project-name” with the name of your project. This will create a new Laravel project in a directory with the same name as your project.
Step 3: Configuring the Database
Next, we need to configure the database for our Laravel project. Open the .env
file in the root directory of your project and modify the following settings to match your MySQL configuration:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password
Save the .env
file when you’re done.
Step 4: Creating a Route
Every web application needs some routes to define entry points into the application. Laravel provides a simple and powerful router that makes it easy to define routes for your application.
Open the routes/web.php
file in your project and add the following line:
Route::get('/', function () {
return view('welcome');
});
This defines a route for the root of the application, which returns a view called “welcome”. We haven’t created this view yet, but we’ll do that in the next step.
Step 5: Creating a View
Views are templates that define the HTML structure of your web pages. In Laravel, views are stored in the resources/views
directory.
Create a new file called welcome.blade.php
in the resources/views
directory with the following contents:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to my web app</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
This defines a simple HTML structure with a h1
element containing the text “Hello, World!”.
Step 6: Running the Application
Now that we have a basic route and view, let’s fire up the Laravel development server and see our web application in action!
In your terminal, navigate to the root directory of your Laravel project and run the following command:
php artisan serve
This will start the development server and provide you with an address where you can view your web application. By default, this will be `http://localhost:8000/`.
Open up your web browser and navigate to the provided address. You should see your “Hello, World!” message displayed in your browser.
Step 7: Working with Migrations
Migrations are a powerful feature in Laravel that allow you to version control your database schema. Migrations make it easy to modify your database schema over time in a way that is controlled and repeatable.
To create a new migration, run the following command:
php artisan make:migration create_users_table
This will create a new migration file in the database/migrations
directory with the name create_users_table.php
.
Open up this file and modify the up
method to define the schema for a users table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
This defines a table with columns for id
, name
, email
, email_verified_at
, password
, remember_token
, and the default Laravel created_at
and updated_at
timestamps.
To run the migration, run the following command:
php artisan migrate
This will create the users
table in your database.
Step 8: Working with Models and Controllers
Models are the data access layer of your Laravel application, while Controllers handle the logic for your application’s routes.
First, let’s create a new model for our users
table. Run the following command:
php artisan make:model User
This will create a new model file in the app
directory with the name User.php
.
Open up this file and modify the fillable
property to include all of the columns in the users
table:
protected $fillable = [
'name', 'email', 'password',
];
Next, let’s create a new controller that will handle requests for our users. Run the following command:
php artisan make:controller UserController --resource
This will create a new controller file in the app/Http/Controllers
directory with the name UserController.php
. The “–resource” flag tells Laravel to create a controller that implements the seven typical RESTful controller actions.
Open up this file and modify the store
method to create a new user:
public function store(Request $request)
{
$input = $request->all();
$user = User::create($input);
return response()->json($user, 201);
}
This method receives a Request
object that contains the data for the new user, creates a new User
object using the $input
data, and returns a JSON response containing the new user object.
Step 9: Working with Routes and Controllers
Now that we have a controller, let’s define some routes that will map to the controller’s actions.
Open the routes/web.php
file and modify it to include the following routes:
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::get('/users/{user}', 'UserController@show');
Route::put('/users/{user}', 'UserController@update');
Route::delete('/users/{user}', 'UserController@destroy');
These routes define our mapping from URLs to our RESTful controller actions.
Step 10: Authenticating Users
Laravel provides built-in support for user authentication, making it easy to secure your web application.
To get started, we need to create a new authentication scaffolding using the following command:
php artisan make:auth
This will create all of the necessary files and routes to handle user authentication in Laravel.
Next, we need to migrate the database to create the necessary authentication tables:
php artisan migrate
This will create the users
and password_resets
tables in your database.
Conclusion
In this tutorial, we’ve covered the basics of building a web application using Laravel. We’ve covered installation, configuration, routing, views, database migrations, and authentication.
With Laravel, you can build powerful and robust web applications quickly and easily. Happy coding!