Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nababur's avatar
Level 43

How can createOrUpdate method

How can i create updateOrCreate method insert data if empty field or update data if available field?

public function updateCandidate(Request $request, string $id)
{
    $candidate = User::findOrFail($id);
    $profile_id = Profile::where('user_id', $id)->get();



    if (!empty($profile_id)) {
        Profile::create([
            'user_id'=> $id,
            'address'=> request('address'),
            'phone'=> request('phone'),

        ]);


    }else{
        Profile::where('user_id', $id)->update([
            'address'=> request('address'),
            'phone'=> request('phone'),
 
          
        ]);
    }


    $candidate->update($request->all());
            
    return redirect()->back()->with('success', 'Info updated Successfully.');
}
0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To achieve the desired functionality of inserting data if the field is empty or updating the data if the field is available, you can make use of the updateOrCreate method provided by Laravel's Eloquent ORM.

Here's an updated version of the code using the updateOrCreate method:

public function updateCandidate(Request $request, string $id)
{
    $candidate = User::findOrFail($id);
    $profile = Profile::updateOrCreate(
        ['user_id' => $id],
        [
            'address' => request('address'),
            'phone' => request('phone'),
        ]
    );

    $candidate->update($request->all());

    return redirect()->back()->with('success', 'Info updated Successfully.');
}

In this solution, the updateOrCreate method is used to find a profile with the given user_id. If it exists, it will update the address and phone fields with the provided values. If it doesn't exist, it will create a new profile with the given user_id and the provided values.

Note that you need to make sure you have the appropriate relationships defined between the User and Profile models for this solution to work correctly.

supptech's avatar

Here is a example:

$flight = Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);
bushrawaqar's avatar

it looks like you're trying to update a candidate's information along with their associated profile information using Laravel. You can achieve this more efficiently by using the updateOrCreate method provided by Eloquent. Here's how you can refactor your code using updateOrCreate:

php Copy code public function updateCandidate(Request $request, string $id) { $candidate = User::findOrFail($id);

// Update candidate's main information
$candidate->update($request->all());

// Update or create the associated profile information
Profile::updateOrCreate(
    ['user_id' => $id],
    [
        'address' => $request->input('address'),
        'phone' => $request->input('phone'),
    ]
);

return redirect()->back()->with('success', 'Info updated Successfully.');

} With the updateOrCreate method, Laravel will check if a profile with the given user_id exists. If it does, it will update the existing profile with the provided data. If no profile with the given user_id exists, it will create a new profile with the provided data.

Please or to participate in this conversation.