mardi 11 décembre 2018

Newly created record not appearing when called from relationship

I have a function which creates a new record in a model called "Onboarding". I want to then call this record via a relationship in the user model by doing auth()->user()->onboarding however this is returning null. Below is a snippet of my function where this is happening:

public function apiSend(StoreOnboarding $request)
    {
        //Update the user's Onboarding details on DB
        $this->store($request);

        //Validate user's onboarding details
        $validation = $this->checkValidation(auth()->user()->onboarding);

}

My store function:

public function store(StoreOnboarding $request)
{
    $onboarding = auth()->user()->onboarding()->updateOrCreate($request->validated());
}

My store function is working correctly and a new record of onboarding is added to the table, however in my apiSend function, I get an error:

"Argument 1 passed to App\Http\Controllers\OnboardingController::checkValidation() must be an instance of App\Onboarding, null given".

Below is a snippet of my checkValidation function:

public function checkValidation(Onboarding $onboarding)
{
    $validator = Validator::make($onboarding->toArray(), [
    //Validation here
    ]);
}

And here is my onboarding relationship in the user model:

public function onboarding()
{
    return $this->hasOne(Onboarding::class, 'user_id', 'id');
} 

I don't understand why auth()->user()->onboarding is not returning the newly created onboarding record and instead returns null?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire