vendredi 15 mai 2020

Laravel/PHP: Combining a GET/POST?

I have 2 separate methods (one is a POST and the 2nd is a GET) as follows and I find myself calling them most of the time together. Is there a way to combine where I can post and return the new response rather than making a separate GET?

public function createOrUpdateUserData() {

   $user = User::where('user_id', '=', user_id())->first();

   if (!$user) {
      $user = new \App\Models\User;
   }

   //updating existing user
   if ($request->input('name')) {
      $user->name = $request->input('name');
   }
   if ($request->input('style')) {
      $user->style = $request->input('style');
   }       
}

public function getUserData() {

   $user = User::where('user_id', '=', user_id())->first();

   if ($user) {
        return response()->json(['status' => 1, 'data' => $user]);
    } else {
        return response()->json(['status' => 0]);
   }   
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire