You probably want to look at view composers:
https://laravel.com/docs/5.3/views#view-composers
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
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"
Perfect! Thanks.
P.S. I also was calling $this-> wrong. Its supposed to be $this->channels = \App\Channel::all();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community