HomeLaravelLaravel 11 CRUD Application Example Tutorial

Laravel 11 CRUD Application Example Tutorial

To make a Laravel 11 CRUD application, you need to install PHP 8.2, composer, and Laravel 11 first.


To start installing Laravel 11 with PHP 8.2, you can follow these steps based on the provided sources:

Install PHP:

Ensure that PHP 8.2 is installed on your system. You can install PHP using XAMPP 8.2 or other methods depending on your operating system

Install Composer:

Download Composer from getcomposer.org, which is a dependency manager for PHP. Follow the installation instructions to set up Composer on your system.

Create a New Laravel Project:

Once PHP and Composer are installed, you can create a new Laravel project by running the following command in your terminal or CMD:

composer create-project laravel/laravel:^11.0 post


This command will create a new Laravel project with version 11.

Start Laravel Development Server:

Navigate to your project directory and start Laravel’s local development server using Artisan’s serve command:

cd post 
php artisan serve

Your Laravel application will be accessible in your web browser at http://localhost:8000

To implement a CRUD (Create, Read, Update, Delete) functionality for a post schema in Laravel 11, you can follow these steps based on the provided sources:

Define Post Schema: Define the schema for your post model. This typically involves creating a migration file to define the structure of your database table.

php artisan make:migration create_posts_table

Generate Model and Migration: Use Laravel’s Artisan command-line tool to generate a model and migration for your post schema. This will create the necessary files to define your post model and its corresponding database table.

php artisan make:model Post -m
  1. Implement CRUD Operations:

By following these steps and referring to the provided sources, you can successfully create a RESTful API CRUD application for a post schema in Laravel 11.

Implement CRUD Operations

In your PostController.php:

public function index()
{
    $posts = Post::all();
    return response()->json($posts, 200);
}

public function store(Request $request)
{
    $post = new Post();
    $post->title = $request->input('title');
    $post->content = $request->input('content');
    $post->save();

    return response()->json(['message' => 'Post created successfully'], 201);
}

public function update(Request $request, $id)
{
    $post = Post::find($id);
    $post->title = $request->input('title');
    $post->content = $request->input('content');
    $post->save();

    return response()->json(['message' => 'Post updated successfully'], 200);
}

public function destroy($id)
{
    $post = Post::find($id);
    $post->delete();

    return response()->json(['message' => 'Post deleted successfully'], 200);
}

API Routes

Define routes in routes/api.php:

Route::resource('posts', 'App\Http\Controllers\PostController');

Now point your browser to http://localhost:8000/api/posts

Share: 

No comments yet! You be the first to comment.
Categories
Archives