DEV Community

Christina Meador
Christina Meador

Posted on

Creating Your First Laravel CRUD App

Laravel is one of the most popular PHP frameworks, known for its elegant syntax and robust features. If you're new to Laravel and want to start developing applications, this guide will walk you through the initial steps, helping you set up your environment and understand the core concepts.

What is Laravel?

Laravel is a web application framework that provides an expressive and straightforward syntax. It follows the MVC (Model-View-Controller) architecture, making it easy to build clean and maintainable code. With built-in features like routing, authentication, and Eloquent ORM, Laravel simplifies the web development process.

Prerequisites

Before you start, ensure you have the following installed:

  • PHP (>= 7.3)
  • Composer (a dependency manager for PHP)
  • A web server (like Apache or Nginx)
  • A database (like MySQL or PostgreSQL)
  • A code editor (e.g., Visual Studio Code, PhpStorm)

Step 1: Install Laravel

To install Laravel, you can use Composer. Open your terminal and run the following command:


composer global require laravel/installer

After the installation, make sure your Composer's global vendor/bin directory is in your system's PATH.

Now, create a new Laravel project:


laravel new my-laravel-app

Or, if you prefer using Composer directly:


composer create-project --prefer-dist laravel/laravel my-laravel-app

Navigate into your new project directory:


cd my-laravel-app

Step 2: Set Up Your Environment

Laravel uses an .env file for environment configuration. This file includes database credentials and other settings. Open the .env file in your project and configure your database settings:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Make sure to create the database you specified in DB_DATABASE using your preferred database management tool.

Step 3: Run the Development Server

Laravel comes with a built-in development server that you can use to test your application. To start it, run:


php artisan serve

You can now access your application by visiting http://127.0.0.1:8000 in your web browser.

Step 4: Understand the Folder Structure

Familiarize yourself with the basic folder structure of a Laravel application:


app/: Contains the core code, including models, controllers, and middleware.
routes/: Where you define application routes.
resources/: Contains views (using Blade templating), JavaScript, and CSS files.
database/: Contains migration and seed files.
config/: Configuration files for your application.
public/: The public-facing directory that contains assets like images, CSS, and JavaScript.

Step 5: Create Your First Route

Open the routes/web.php file to define your first route. Add the following code:


Route::get('/', function () {
return view('welcome');
});

This route will return the welcome view when you visit the home page.

Step 6: Create a Controller

Controllers handle the application logic. You can create one using Artisan:


php artisan make:controller HomeController

Open the newly created controller in app/Http/Controllers/HomeController.php and add a method:


public function index()
{
return view('home');
}

Next, update your route to use the controller:

`
use App\Http\Controllers\HomeController;

Route::get('/', [HomeController::class, 'index']);
`

Step 7: Create a View

Create a new view file in resources/views called home.blade.php:


<!DOCTYPE html>
<html>
<head>
<title>Welcome to Laravel</title>
</head>
<body>
<h1>Hello, Laravel!</h1>
</body>
</html>

Now, when you visit the home page, you should see the "Hello, Laravel!" message.

Step 8: Explore Eloquent ORM

Eloquent is Laravel's built-in ORM that makes database interactions easy. You can define a model and use it to interact with your database.

Create a new model and migration for a Post:


php artisan make:model Post -m

In the migration file located in database/migrations, define the posts table structure:


public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}

Run the migration:


php artisan migrate

You can now use the Post model to interact with the posts table in your database.

Step 9: Use Artisan Commands

Laravel’s Artisan command-line interface provides various helpful commands. You can see a list of available commands by running:


php artisan list

Some useful commands include:


php artisan make:model ModelName – Creates a new model.
php artisan make:controller ControllerName – Creates a new controller.
php artisan migrate – Runs database migrations.

Step 10: Explore Laravel Documentation

Laravel has comprehensive documentation that covers almost everything you need to know. Spend time exploring topics such as:

  • Routing
  • Middleware
  • Blade templating
  • Authentication
  • Eloquent ORM

Top comments (0)