vendredi 13 avril 2018

Laravel return empty relationship on model when condidtion is true

I have a model where I want to return a empty value when a condition happens, but when I try to get the model:

Model::with('shop')->find(id);

I get this error:

"Call to a member function addEagerConstraints() on null"

This is the code I'm trying:

public function shop(){
    if(true) {
        return null;
    }
    return $this->belongsTo('App\Models\Shop');
}

How is the proper way to return nothing when a condition is true on Laravel relationships?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire