bluebeard
4 months ago
465
5
Laravel

Update Or Create

Posted 4 months ago by bluebeard

The user has a profile setting. I want if the user changes some fields to be updated. But I have a new column created and should be updated. Maybe someone is not doing it right. Help please. Thank you very much.

Task: I will tell you my task. the user has a setting profile model. When the profile of the new field is not yet filled. When it is filled, the column in the base is displayed. But maybe something will change over time. I need to update the same column, and not create a new column.

Controller:

 public function profile_settings_post(Request $request){
      // Auth Specialist
      $user = Auth::user();
      // Data Specialist Validate
      $data = $request->validate([
          'first_name' => 'nullable|string',
          'last_name' => 'nullable|string',
          'phone_number' => 'nullable|integer',
          'gender' => 'nullable',
          'date_of_birth' => 'nullable',
          'about_me' => 'nullable',
          'address' => 'nullable',
          'city' => 'nullable|string',
          'country' => 'nullable|string',
          'postal_code' => 'nullable|integer',
      ]);
        $profile = $user->profile_settings()->updateOrCreate($data);
        $profile->save();

      // RETURN REDIRECT PROFILE SETTINGS INDEX
    return redirect()->route('frontend.specialist.profile.settings');
}

Please sign in or create an account to participate in this conversation.