0

Code is fine and I'll double check my code but I didn't find anything wrong in my code. This doesn't update or insert address or new user data in any condition.

$user = User::updateOrCreate(
 [
 'id' => Auth::id(),
 'address' => auth()->user()->address
 ],
 [
 'address' => $request->address
 ]
);
if ($user)
 return response()->json([
 'status' => 200,
 'add' => $request->address,
 'msg' => 'Address Updated Successfully'
 ]);
else
 return response()->json([
 'status' => 200,
 'msg' => 'Error Occured'
 ]);

Same Code here just taking user_id in parameter

$user = User::updateOrCreate(
 [
 'id' => $request->id
 ],
 [
 'address' => $request->address
 ]
);
if ($user)
 return response()->json([
 'status' => 200,
 'add' => $request->address,
 'msg' => 'Address Updated Successfully'
 ]);
else
 return response()->json([
 'status' => 200,
 'msg' => 'Error Occured'
 ]);
IGP
16.4k5 gold badges35 silver badges56 bronze badges
asked May 15, 2022 at 0:59
6
  • 1
    Can you show what's coming in Auth::id()? Commented May 15, 2022 at 1:47
  • 1
    Try adding address to your User Model $fillable ? Commented May 15, 2022 at 2:00
  • Check if the controller action is within the auth middleware to obtain Auth::id(). For user_id in parameter, shouldn't it be $request->user_id ? Commented May 15, 2022 at 5:49
  • Authenticated user id is coming up in Auth::id(). @parth Commented May 15, 2022 at 6:37
  • yeah let me try model $filable thing for address that may work for me @jef Commented May 15, 2022 at 6:38

1 Answer 1

0

If you use "updateOrCreate" method in UserController, this is not a good practice to make them in the same endpoint. Normally, "updateOrCreate" should be used for secondary type of data. For main data, creating and updating should hit the separate endpoints thus separate methods.

Furthermore, If you have Auth::id(), then you have a user. So there is nothing to create. To update the user, just use update method and get the user with id.

To create a user, use the "store" method pass the values and create it there.

answered May 15, 2022 at 6:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.