About Us Blog Contact Us Request a quote

Laravel 12 Build Rest API Resource

Install Laravel 12 App


composer create-project laravel/laravel example-app

Setup mysql database

update .env file


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel12app //database name
DB_USERNAME=root
DB_PASSWORD=

migrate database


php artisan migrate

Create Model & Migration


php artsan make:model Contact -m

app\Models\Contact.php like as below code


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = [
        'name',
        'email',
        'phone',
        'address',
    ];

    public static function getData()
    {
        $data = self::get();
        return $data;
    }

    public static function createData($request)
    {
        self::create([
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'address' => $request->address,
        ]);
    }

    public static function editData($id)
    {
        $data = self::find($id);
        return $data;
    }

    public static function updateData($request, $id)
    {
        self::where('id', $id)->update([
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'address' => $request->address,
        ]);
    }

    public static function deleteData($id)
    {
        self::where('id', $id)->delete();
    }
}

database\migrations\2025_02_27_075816_create_contacts_table.php like as below code


<?phpp

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone')->unique();
            $table->text('address');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('contacts');
    }
};

Run below this command


php artisan migrate

Create API route

Install laravel API


php artisan install:api

routes/api.php like as below code


<?php

use App\Http\Controllers\API\ContactController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:sanctum');

Route::resource('contacts', ContactController::class);

Create API resource


php artisan make:ContactResource

App/controller/Resources/ContactResource.php like as below code


<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ContactResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return parent::toArray($request);
    }
}

Create API controller


php artisan make:controller API/ContactController --resource

App/controller/API/ContactController.php like as below code


<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data = Contact::getData();
        return new ContactResource($response);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        Contact::createData($request);
        $response = [
            'success' => true,
            'message' => 'Contact successfully added.'
        ];
        return response()->json($response);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $data = Contact::editData($id);
      return new ContactResource($response);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        Contact::updateData($request, $id);
        $response = [
            'success' => true,
            'message' => 'Contact successfully added.'
        ];
        return response()->json($response);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        Contact::deleteData($id);
        $response = [
            'success' => true,
            'message' => 'Contact deleted.'
        ];
        return response()->json($response);
    }
}

Run Laravel App:

Now Run your laravel 12 app after run these below commands


php artisan serve

Go to your postman and check the following APIs.

1- Contact Listing API: Verb:GET, URL:http://localhost:8000/api/contacts



2- Contact Edit API: Verb:GET, URL:http://localhost:8000/api/contacts/{id}/edit




3- Contact Create API: Verb:POST, URL:http://localhost:8000/api/contacts/


4- Contact Update API: Verb:PUT, URL:http://localhost:8000/api/contacts/{id}



5- Contact Delete API: Verb:DELETE, URL:http://localhost:8000/api/contacts/{id}


Recent Posts