mercredi 6 mars 2019

HasMany relation(or any relation) data inserted but not return on the same time | Laravel | Eloquent Model

I have a laravel application in which the setting_types and user's settings are saved into different models.

User.php
// user model.
/*
 * Getting the user's notification setting.
*/
public function notificationSetting()
{
    return $this->hasMany('App\NotificationSetting');
}

/*
 * Controller function to get the user's settings
*/
public function getSetting(Request $request)
    {
        ...
        $userSetting = $user->notificationSetting;
        // check new settings are inserted for user or not.
        if(someCondition){
            ...
            // add new settings for user.
            $user->notificationSetting()->save(new NotificationSetting(['user_id' => $user_id, "notification_type_id" => 121]));
            print_r($user->notificationSetting); // still rec. Old values.
            ...
        }
        return $user->notificationSetting;
    }

As you can see that I insert the relation object but I didn't receive on the same time. and if I hit again (this time my someCondition become false) so it will return the update records.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire