I am trying to make the value of $input data to first match with one the pluck array values in a user table in the database before the input is saved and a response of 'its a match be given'. I trying to put these checks on an API I'm making. I'm new to laravel API and I will appreciate the help. Thank you
public function create(Request $request) { // $this->validate($request, [ 'inputed_number' => 'required|unique:users', 'user_id' => 'required' ]); try { Auth::user(); $phonenumber = new PhoneNumber(); $phonenumber->inputed_number = $request->inputed_number; $phonenumber->user_id = $request->user_id; if($this->user->phonenumber()->save($phoneuumber)){ $user = User::all(); $input = $request->only('inputed_number'); $user_number = $user->pluck('phone'); foreach ($input as $phone) { if ($phone !== $user_number) { return response()->json([ 'success' => false, 'message' => 'Invalid number', 'data' => $phone, ], Response::HTTP_UNAUTHORIZED); } return response()->json([ 'success' => true, 'message' => 'Its a match!!', 'data' => $phone, ]); } } } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => 'You have already inputted this number.', ], Response::HTTP_UNAUTHORIZED); } }
https://stackoverflow.com/questions/65802107/how-to-match-an-input-value-in-laravel-match-before-saving-to-sqlite-database January 20, 2021 at 10:06AM
没有评论:
发表评论