Using multiple models in one shared blade template

Posted 4 months ago by oliverbusk

Hi there

OK, so consider below navigation, which is stored in a blade template:

menu.blade.php:

<ul class="nav flex-column leftbar">
    <li class="nav-item">
        <a class="nav-link" href="{{ $stream->path() }}">Overview</a>
    </li>
</ul>

So, as you can see, above references the $stream variable.

The $path function is inside my app\Stream model:

 public function path()
 {
      return '/streams/' . $this->token;
 }

Now I can use the menu/navigation whenever I view a specific resource - for example in my StreamsController:

public function show(Stream $stream)
{
      return view('streams.show', compact('stream'));
}

Now the problem arises when I have a new model. This is called app\Documents.php. The relationships are like this:

A stream has many documents. A document belongs to a stream.

If I go to a page, that uses the DocumentsController, I get this error:

Undefined variable: stream (View: /Users/user/Sites/playground/resources/views/streams/nav/menu.blade.php) 

What am I doing wrong?

Please sign in or create an account to participate in this conversation.