/ProductController.php Secret
Created
September 24, 2022 07:23
Laravel Product Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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