Support the ongoing development of Laravel.io →
Database Views Blade
Last updated 2 years ago.
0

You probably want to look at view composers:

https://laravel.com/docs/5.3/views#view-composers
0

Elite123,

Those instructions worked great! Well, almost. I can display a local array but cannot seem to display a list of pulled from mysql and the model. This works..

namespace App\Http\ViewComposers;

use DB;
use Illuminate\View\View;

class ChannelComposer
{
    public $movieList = [];
    /**
     * Create a movie composer.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->$channels = App\Channel::all();

        $this->channels = [
            'Amazon',
            'eBay',
            'Jet',
        ];
    }
    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('channels', $this->channels);
    }
}

This does not..


public function __construct()
{
    $this->$channels = App\Channel::all();

    /*
    $this->channels = [
        'Amazon',
        'eBay',
        'Jet',
    ];
    */

}

Here is the error I get..

Whoops, looks like something went wrong.
3/3 ErrorException in ChannelComposer.php line 18: Class 'App\Http\ViewComposers\App\Channel' not found (View: /Users/chris/Dropbox/Code/Laravel/oats/resources/views/layouts/default.blade.php) (View: /Users/chris/Dropbox/Code/Laravel/oats/resources/views/layouts/default.blade.php) 

Somehow I am not able to call the Channel model. If I run "$channel = App\Channel::all()" in tinker it works great.

Thanks again for any help,

Chris

Last updated 8 years ago.
0

Hi Chris

Looks like a namespacing - try:

$this->$channels = \App\Channel::all();

notice the the backslash before "App\Channel" - this tells it to reference the class from the root namespace (e.g. "App\Channel", rather than look for "App\Channel" in "\App\Http\ViewComposers"

0

Perfect! Thanks.

P.S. I also was calling $this-> wrong. Its supposed to be $this->channels = \App\Channel::all();

0

Sign in to participate in this thread!

Eventy

Your banner here too?

offlinetn offlinetn Joined 4 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.