Laravel how to create an empty relationship?

I have a condition inside the relationship, where if the user is logged in, he will return the relationship, and if I do not want him to return an empty relation. here is what i want:

public function dummy() { return (auth()->user()) ? $this->hasOne(blah::class) : emptyrelationship(); } 
+6
source share
2 answers

You should check with DD () that it returns as you like.

If there is no data to show the relationship, they simply will not return data.

0
source

Try this example.

 public function shop(){ if(true) { return $this->newQuery(); // or newQueryWithoutScopes() } return $this->belongsTo('App\Models\Shop'); } 
0
source

All Articles