Create Laravel 12 project from scratch

  • avatar
  • 114 Views
  • 6 mins read

Laravel is a PHP framework built to make web development cleaner and faster by following the Model-View-Controller (MVC) pattern. Since its first release in 2011, it has grown into one of the most popular choices for PHP developers. Laravel brings powerful tools like expressive routing, simple database migrations, a built-in authentication system, and Blade templating, making it easier to build full-featured applications without getting buried in repetitive code.

Prerequisites

Before you can create a Laravel 12 project, make sure your machine is ready. You will need PHP 8.2 or newer, Composer installed globally, and access to a database system like MySQL, PostgreSQL, or SQLite. If you are missing any of these, it's worth setting them up properly first. Composer is especially important since Laravel manages all its dependencies through it.

Installation

Assuming Composer is installed on your system, run the following command to create a new Laravel project. It will generate a fresh project inside a directory named laravel12_project and automatically install the required dependencies.

composer create-project laravel/laravel:^12.0 laravel12_project

Configuring Laravel

Laravel requires minimal configuration out of the box, so you can start developing immediately. However, it's recommended to check the config/app.php file and its documentation. This file contains several settings that may need to be customized to fit your application's specific requirements.

Environment variables

Environment variables store sensitive information like passwords, credentials, and other private data that shouldn’t be hard-coded into the source code. They are crucial for managing settings that may change across different environments. Laravel provides a clear example of how to structure your initial environment file in the .env document. You'll need to modify this file based on your specific configuration needs and requirements.

Setting folder permissions

In a Laravel application, the storage and bootstrap/cache directories are essential for storing logs, cache files, and compiled views. Laravel requires write access to these directories in order for the application to run properly. To ensure Laravel can properly write to these directories, you need to set the correct permissions:

  1. Navigate to the root of your Laravel project.

  2. Run the following command to grant write permissions to these folders:
    sudo chmod -R 775 storage bootstrap/cache
    This will give the web server (and the application) permission to write data to these directories.

  3. Optionally, change the ownership of these directories to match the web server's user (usually www-data for Apache or Nginx). This ensures that the web server can access and write to the folders:
    sudo chown -R www-data:www-data storage bootstrap/cache

By doing this, you'll avoid permission issues that could cause errors when the application tries to log data, handle file uploads, or cache information.

Databases and Migrations

After setting up your Laravel application, you’ll likely need to store data in a database. By default, the .env configuration file is set to use a SQLite database. Laravel will automatically create a database/database.sqlite file and run migrations to create the necessary database tables.

If you prefer to use a different database, like MySQL or PostgreSQL, you can update the .env file with the appropriate settings. For example, to switch to MySQL, simply modify the DB_* variables in the .env file and then run your application's migrations:

php artisan migrate

Using Laravel

Once your application is configured, you can start Laravel's local development server. Simply navigate to the project directory and run the following Artisan command to launch the server:

php artisan serve

While the server is running, the terminal will remain active and display the URL you can use to access the application in any browser on your system. Typically, the URL will look like this: http://127.0.0.1:8000.

Laravel local development serverTo unlock the terminal and stop the execution use the Ctrl+C combination on our keyboard.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.