Laravel is a powerful PHP framework that simplifies web application development. To get started with Laravel, you need to set up a local development environment for Laravel that matches your production setup. Here's how you can do it.
1. Installing Prerequisites
Before diving into Laravel, ensure you have the following installed:
- PHP: Laravel requires PHP 7.4 or higher. You can install PHP via your package manager or download it directly from php.net.
- Composer: This dependency manager is crucial for handling Laravel’s packages. Download and install it from getcomposer.org.
2. Creating a New Laravel Project
With PHP and Composer ready, you can create a new Laravel project using Composer:
composer create-project --prefer-dist laravel/laravel my-project
Replace "my-project"
with your desired project name. This command installs Laravel and its dependencies.
3. Setting Up a Local Server
Laravel offers several ways to serve your project locally:
PHP’s Built-in Server: This is the simplest method. Run
php artisan serve
on your console. Your project will be accessible at http://localhost:8000.Laravel Valet: A great option for Mac users, Valet provides a fast and minimalistic local environment. Follow the installation guide on Laravel's official site.
Homestead: Laravel Homestead is a pre-packaged Vagrant box that provides a development environment similar to your production setup. It’s ideal for those who need a complete and isolated environment.
Docker: For a containerized approach, Docker allows you to run your Laravel project in an isolated environment. You can use official Docker images or create a custom Dockerfile.
4. Configuring Environment Variables
Laravel uses a .env
file for configuration. Update this file with your local settings, such as database credentials, to match your local setup:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
5. Setting Up the Database
Ensure that your database server is running and accessible. Update your .env file with the correct database credentials. If you're using MySQL, you can create a new database for your Laravel project:
CREATE DATABASE my_project;
Connect to the database by updating the .env file with the database name, user, and password.
6. Running Migrations
Laravel uses migrations to manage your database schema. Run the migrations to set up your tables:
php artisan migrate
This command will create the necessary tables based on the migration files in your project.
Conclusion
With your local environment set up, you're ready to start developing your Laravel application. This setup will ensure your development process is smooth and closely mirrors your production environment.
You can check another tutorial below:
Blog: https://codenoun.com
Telegram: https://t.me/s/codenoun
Top comments (0)