Skip to content

Instantly share code, notes, and snippets.

@ualmtorres
Created September 24, 2022 07:23
Show Gist options
  • Save ualmtorres/2c92fe219534f50701358b3b38683092 to your computer and use it in GitHub Desktop.
Save ualmtorres/2c92fe219534f50701358b3b38683092 to your computer and use it in GitHub Desktop.
Laravel Product Controller
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
try {
$products = Product::all();
} catch (Exception $e) {
return response()->json([
'data' => [],
'message'=>$e->getMessage()
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
return response()->json([
'data' => $products,
'message' => 'Succeed'
], JsonResponse::HTTP_OK);
}
public function show($id)
{
try {
$products = Product::find($id);
} catch (Exception $e) {
return response()->json([
'data' => [],
'message'=>$e->getMessage()
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
return response()->json([
'data' => $products,
'message' => 'Succeed'
], JsonResponse::HTTP_OK);
}
public function store(Request $request)
{
try {
$products = Product::create($request->all());
} catch (Exception $e) {
return response()->json([
'data' => [],
'message'=>$e->getMessage()
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
return response()->json([
'data' => $products,
'message' => 'Succeed'
], JsonResponse::HTTP_OK);
}
public function update(Request $request, $id)
{
try {
$products = Product::find($id)
->update($request->all());
} catch (Exception $e) {
return response()->json([
'data' => [],
'message'=>$e->getMessage()
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
return response()->json([
'data' => $products,
'message' => 'Succeed'
], JsonResponse::HTTP_OK);
}
public function destroy($id)
{
try {
$products = Product::destroy($id);
} catch (Exception $e) {
return response()->json([
'data' => [],
'message'=>$e->getMessage()
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
return response()->json([
'data' => $products,
'message' => 'Succeed'
], JsonResponse::HTTP_OK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment