Download Laravel App
https://laravel.com/docs/11.x/installation
Connecting our Database
open .env file root directory.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=laravel11dev
DB_USERNAME=root
DB_PASSWORD=root
install laravel sanctum
https://laravel.com/docs/11.x/sanctum#main-content
php artisan install:api
C:\xampp\htdocs\laravel\myapp>php artisan install:api
Edit User Model and add API Tokens
app/Models/User.php
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
}
Create Controller php artisan make:controller API/ApiController
C:\xampp\htdocs\laravel\myapp>php artisan make:controller API/ApiController
app\Http\Controllers\API\ApiController.php
//app\Http\Controllers\API\ApiController.php <?php namespace App\Http\Controllers\API; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\User; use GuzzleHttp\Promise\Create; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; class ApiController extends Controller { public function register(Request $request) { try { $validateuser = Validator::make($request->all(), [ 'name' => 'required', 'email' => 'required|email|unique:users,email', 'password' => 'required', ] ); if ($validateuser->fails()) { return response()->json([ 'status' => false, 'message' => 'validation error', 'errors' => $validateuser->errors() ],401); } $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => $request->password, ]); return response()->json([ 'status' => true, 'message' => 'User created Succesfully', 'token' => $user->createToken('API TOKEN')->plainTextToken ], 200); } catch (\Throwable $th) { // Return Json Response return response()->json([ 'status' => false, 'message' => $th->getMessage(), ], 500); } } public function login(Request $request) { try { $validateuser = Validator::make($request->all(), [ 'email' => 'required|email', 'password' => 'required', ] ); if ($validateuser->fails()) { return response()->json([ 'status' => false, 'message' => 'validation error', 'errors' => $validateuser->errors() ], 401); } if (!Auth::attempt(($request->only(['email','password'])))) { return response()->json(['status' => false, 'status' => false, 'message' => 'Something went really wrong!', ],401); } $user = User::where('email', $request->email)->first(); return response()->json([ 'status' => true, 'message' => 'Succesfully login', 'token' => $user->createToken('API TOKEN')->plainTextToken ], 200); } catch (\Throwable $th) { // Return Json Response return response()->json([ 'status' => false, 'message' => $th->getMessage(), ], 500); } } public function profile() { // Profile Detail $userData = auth()->user(); // Return Json Response return response()->json([ 'status' => true, 'message' => 'Profile Info', 'data' => $userData, 'id' => auth()->user()->id, ], 200); } public function logout() { auth()->user()->tokens()->delete(); // Return Json Response return response()->json([ 'status' => true, 'message' => 'Successfully Logout', 'data' => [] ], 200); } }Create Model and Migration
C:\xampp\htdocs\laravel\laravelproject>php artisan make:model Product -m
A new file named Product.php will be created in the app directory and database/migrations directory to generate the table in our database
app/Models/Product.php
//app/Models/Product.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; protected $fillable = [ 'name', 'image', 'price' ]; }database\migrations\create_products_table.php
//database\migrations\create_products_table.ph <?php 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('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('image'); $table->integer('price'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('products'); } };Database Migration
php artisan migrate
C:\xampp\htdocs\laravel\laravel10project>php artisan migrate
Migration table created successfully.
check database table
Create Controller and Request
C:\xampp\htdocs\laravel\laravel10project>php artisan make:controller ProductController
app\Http\Controllers\ProductController.php
//app\Http\Controllers\ProductController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; use App\Http\Requests\ProductStoreRequest; use Illuminate\Support\Str; use Illuminate\Support\Facades\Storage; //php artisan storage:link = php artisan storage:link = http://127.0.0.1:8000/storage/1.jpg class ProductController extends Controller { public function index() { //$products = Product::all(); // All Product $products = Product::paginate(5); // Return Json Response return response()->json([ 'products' => $products ], 200); } public function store(ProductStoreRequest $request) { try { $name = $request->name; $price = $request->price; $imageName = Str::random(32) . "." . $request->image->getClientOriginalExtension(); Storage::disk('public')->put($imageName, file_get_contents($request->image)); Product::create([ 'name' => $name, 'image' => $imageName, 'price' => $price ]); // Return Json Response return response()->json([ 'results' => "Product successfully created. '$name' -- '$imageName' -- '$price' " ], 200); } catch (\Exception $e) { // Return Json Response return response()->json([ 'message' => "Something went really wrong!" ], 500); } } public function show($id) { // Product Detail $product = Product::find($id); if (!$product) { return response()->json([ 'message' => 'Product Not Found.' ], 404); } // Return Json Response return response()->json([ 'product' => $product ], 200); } public function update(ProductStoreRequest $request, $id) { try { // Find product $product = Product::find($id); if (!$product) { return response()->json([ 'message' => 'Product Not Found.' ], 404); } echo "request : $request->image"; $product->name = $request->name; $product->price = $request->price; if ($request->image) { // Public storage $storage = Storage::disk('public'); // Old iamge delete if ($storage->exists($product->image)) $storage->delete($product->image); // Image name $imageName = Str::random(32) . "." . $request->image->getClientOriginalExtension(); $product->image = $imageName; // Image save in public folder $storage->put($imageName, file_get_contents($request->image)); } // Update Product $product->save(); // Return Json Response return response()->json([ 'message' => "Product successfully updated." ], 200); } catch (\Exception $e) { // Return Json Response return response()->json([ 'message' => "Something went really wrong!" ], 500); } } public function destroy($id) { // Detail $product = Product::find($id); if (!$product) { return response()->json([ 'message' => 'Product Not Found.' ], 404); } // Public storage $storage = Storage::disk('public'); // Iamge delete if ($storage->exists($product->image)) $storage->delete($product->image); // Delete Product $product->delete(); // Return Json Response return response()->json([ 'message' => "Product successfully deleted." ], 200); } }php artisan make:request ProductStoreRequest
C:\xampp\htdocs\laravel\myapp>php artisan make:request ProductStoreRequest
app\Http\Requests\ProductStoreRequest.php
//app\Http\Requests\ProductStoreRequest.php <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class ProductStoreRequest extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { //return false; return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { if (request()->isMethod('post')) { return [ 'name' => 'required|string|max:258', 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'price' => 'required|string' ]; } else { return [ 'name' => 'required|string|max:258', 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'price' => 'required|string' ]; } } public function messages() { if (request()->isMethod('post')) { return [ 'name.required' => 'Name is required!', 'image.required' => 'Image is required!', 'price.required' => 'Price' ]; } else { return [ 'name.required' => 'Name is required!', 'price.required' => 'Price is required!' ]; } } }routes/api.php
//routes/api.php <?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use App\Http\Controllers\ProductController; use App\Http\Controllers\API\ApiController; Route::post('register', [ApiController::class, 'register']); Route::post('login', [ApiController::class, 'login']); Route::group([ "middleware" => ["auth:sanctum"] ], function() { //profile page Route::get('profile', [ApiController::class, 'profile']); //logout Route::get('logout', [ApiController::class, 'logout']); //product page Route::get('products', [ProductController::class, 'index']); Route::post('products', [ProductController::class, 'store']); Route::get('products/{id}', [ProductController::class, 'show']); Route::put('productsupdate/{id}', [ProductController::class, 'update']); Route::delete('productdelete/{id}', [ProductController::class, 'destroy']); }); //Route::get('/user', function (Request $request) { // return $request->user(); //})->middleware('auth:sanctum');Run C:\xampp\htdocs\laravel\myapp>php artisan serve
Starting Laravel development server: http://127.0.0.1:8000/api/products
generate symbolic links C:\xampp\htdocs\laravel\myapp>php artisan storage:link
Run C:\xampp\htdocs\laravel\myapp>php artisan serve
Starting Laravel development server: http://127.0.0.1:8000
open postman new request
User Register
POST : http://127.0.0.1:8000/api/register?name=cairocoders&email=cairocoders@gmail.com&password=123456789
User Login
http://127.0.0.1:8000/api/login?email=cairocoders@gmail.com&password=123456789
User Profile
GET : http://127.0.0.1:8000/api/profile?email=cairocoders@gmail.com&password=123456789
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
LogOut
GET : http://127.0.0.1:8000/api/logout?email=cairocoders@gmail.com&password=123456789
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
Products
GET api/products Index All products return.
GET : http://127.0.0.1:8000/api/products
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
GET api/products/{id} Show Detail of a particular post by ID.
GET : http://127.0.0.1:8000/api/products/1
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
POST api/products Store Create a new product.
POST : http://127.0.0.1:8000/api/products
body
key value
name Iphone 13
image iphone.jpg = file
price 45
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
PUT api/products/{id} Update Update a particular product by ID.
POST : http://127.0.0.1:8000/api/products/1
body
key value
_method PUT
name Iphone 13 updated
image iphone.jpg = file
price 46 updated
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
DELETE api/products/{id} Destroy Delete a particular product by ID.
DELETE : http://127.0.0.1:8000/api/products/1
Headers
Key Value
Accept application/json
Authorization Bearer 4|0iwwY1hyRu83B9GyfnAuGIDzHPl57BlsaPf4mkkeeb020ff9
https://github.com/cairocodes/Laravel-11-Auth-Api-Login-Register-Profile-Logout-with-CRUD-Product-and-upload-Image/