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.