1

I have had this problem many times now and I really wonder why this is happening.

Here is its latest occurrence:

public function updateBirthDate(Request $request)
{
    try {
        $request->validate([
            'email' => 'required|email',
            'birthDate' => 'required|date_format:Y-m-d',
        ]);
        $user = User::where('email', $request->email)->first();
        $user->profile()->update(['birth_date' => Carbon::parse($request->birthDate)->timestamp]);
        return response_success([], __FUNCTION__, __('verification.user_data_updated'));
    } catch (Exception $exception) {
        Log::error(__FUNCTION__, compact('exception'));
        return response_error(__FUNCTION__, $exception->getMessage());
    }
}

The code above didn't change 'birth_date' in database when I sent the request through Postman.

But when I ran the line below in tinker, it changed 'birth_date' in database:

$user->profile()->update(['birth_date' => Carbon::parse('1990-01-01')->timestamp]);

I suppose this is a bug in this Laravel version.

7
  • 2
    What data are you sending via PostMan? Did you get Validation errors? Did you try without $request->validate()? I can see a lot of points of failure with your code that wouldn't be there if you simply did User::where('email', '[email protected]')->first();, then $user->profile->update([...]) in tinker. Might be worth editing your question and provide some more clarification. Commented Oct 8, 2024 at 18:42
  • @TimLewis I was sure about rest of the code but now I'm in doubt, I'm going to check those. By the way, this is not the first time I'm facing this issue, so either in all those occurrences rest of the code contained some problem which I didn't notice or update method is really problematic in this Laravel version. I'm going to test it all. Commented Oct 9, 2024 at 9:35
  • 2
    No worries! I guess I should clarify; $request->validate() can send an HTTP redirect to PostMan if the headers being sent are incorrect, User::where(...)->first() can return null, and null->profile()->update() would fail. $user->profile() is a Builder instance, not a single Profile instance, and it could simply silently fail if the User doesn't have a Profile, or update the wrong one if the User has multiple profiles (I think; I've never tried $user->profile()->update() vs $user->profile->update). That's what I meant by "many possible points of failure". Commented Oct 9, 2024 at 12:58
  • 1
    @TimLewis you may be right, I just tested, I don't know why but now it's working through Postman request and with both versions: $user->profile() and $user->profile. I am going to edit my question text now to include the whole method. As you will see, there is exception handling but there wasn't an exception in either response or log file. $user->profile() returns Illuminate\Database\Eloquent\Relations\HasOne Eloquent relationship class by the way. Commented Oct 10, 2024 at 15:42
  • 1
    Haha, all good; I've lost track the number of times I've slept on an issue only for it to magically resolve itself 😅 Either way, glad it's working now, and if you run into it again, you know what to do. Cheers! Commented Oct 10, 2024 at 17:08

1 Answer 1

0

I have noticed the reason when one of my methods with update didn't work but another one worked.

The following didn't work:

$user->update([
    'confirmation_code' => random_int(100000, 999999),
    'email' => $request->email,
]);

The following worked:

$user->update([
    'mail_confirmation' => true
]);

I have missed a basic point.

The second one works because 'mail_confirmation' field is in $fillable array but first one doesn't work because 'confirmation_code' isn't in $fillable array.

Update is one of the mass assignment methods of Laravel Eloquent ORM Model. Others are fill and create: https://laravel.com/docs/10.x/eloquent#allowing-mass-assignment

So, all the fields must be in $fillable array unless $guarded array is set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.