Lectures List
capscom technology computer course almora

Restful Api in Laravel9 with sanctum authentication

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // http://127.0.0.1:8000/api/users    (GET)
    public function index()
    {
        $user=new User();
        return response()->json([
            "status"=>200,
            "data"=>$user::all(),
            "message"=>"success"
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $name=$request->input("name");
        $email=$request->input("email");
        $password=$request->input("password");        
        $user= new User();
        $user->name=$name;
        $user->email=$email;
        $user->password=bcrypt($password);
        $user->save();
        return response()->json([
            "status"=>200,
            "message"=>"Account created successfully"
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // http://127.0.0.1:8000/api/users/1    (GET)
    public function show($id)
    {
        $model=new User();
        $user=$model->find($id);  
        return response()->json([
            'status'=>200,
            'data'=>$user,
            'message'=>'Success'
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
// http://127.0.0.1:8000/api/users/1?name=ankit&email=ankit@gmail.com    (PUT)
    public function update(Request $request, $id)
    {
        $model=new User();
        $name=$request->input("name");
        $email=$request->input("email");
        $user=$model::find($id);
        $user->name=$name;
        $user->email=$email;
        $user->save();
        return response()->json([
            'status'=>200,
            'message'=>'Updated'
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
// http://127.0.0.1:8000/api/users/1     (DELETE)
    public function destroy($id)
    {
        $model=new User();
        $user=$model::find($id);
        $user->delete();
        return response()->json([
            'status'=>200,
            'message'=>'Deleted successfully'
        ]);
    }
}

Routes

Route::apiResource('users', UserController::class)
->middleware(['auth:sanctum']);
Author/Written by: Mr Hemant Singh (Founder)
Terms & Conditions CAPSCOM TECHNOLOGY Pvt. Ltd. का बेहतर रूप से संचालन करने के लिए यहॉ कुछ नियमें और शर्ते लागू है अगर आप कैप्सकॉम टैक्नोलॉजी में पढ़ रहे है तो यह आप पर लागू होता है।