Browse Source

add unique field name

pull/3/head
wahyuun 1 year ago
parent
commit
4f4682c48f
  1. 7
      app/Http/Controllers/RoleController.php

7
app/Http/Controllers/RoleController.php

@ -11,7 +11,7 @@ class RoleController extends Controller
public function add(Request $request) public function add(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'name' => 'required', 'name' => 'string|required|unique:m_roles,name',
'description' => 'required' 'description' => 'required'
]); ]);
@ -44,7 +44,12 @@ class RoleController extends Controller
if(empty($id) || !is_int((int)$id)) if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
$this->validate($request, [
'name' => 'string|required',
'description' => 'required'
]);
$data = Role::find($id); $data = Role::find($id);
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_roles,name']) : '';
if(!$data) if(!$data)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>404], 404); return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>404], 404);

Loading…
Cancel
Save