Hi,
I'm really really new to Laravel (and up-to-date frameworks in general) and still doing the basic tutorials, but I got a similar question to such relationships.
The foreign id isn't always set, but nevertheless I want to use "belongsTo". On the other side, it is throwing me errors in the view, when I don't check the value of the forein id first.
To use the above example, is it a good idea to do something like this?
class Post extends Model
{
public function editor()
{
return ($this->editor_id > 0) ? $this->belongsTo(User::class, 'editor_id') : new User;
}
}
Edit: My suggestion is by the way not working. :( As a return type an instance of a relationship class is expected...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community