Following the guideline, I have
1) Create a service provider
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
view()->composer('template', function ($view) {
$view->with('series_list', Series::all());
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
2) Register provider
App\Providers\ViewComposerServiceProvider::class,
3) And echo the variable at the template
{!! var_dump($series_list) !!}
The problem is :
Route::get('/', 'HomeController@index');
Route::get('product/view/{id}', 'ProductController@view');
Route::get('product/detail/{id}', 'ProductController@detail');
Route::get('/page/contact', 'PageController@contact');
PageController and HomeController can show the $series_list, but ProductController will return error:
Undefined variable: series_list in the template
Here is the ProductController:
class ProductController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function view($id = 1)
{
return view('product/list');
}
public function detail($id = 1)
{
return view('product/detail');
}
}
Thanks a lot for helping.
Laravel provides different ways to pass data to a view. We can pass data directly from routes or through the controller. Here are some of the ways we can pass data to the view: 1. Using view (): We can directly pass the data in the ‘ view () ’ helper function by using the second parameter in the function which takes an array as key and value pair.
To customize laravel you will need to take the following steps: Show data with pagination Make model: To fetch data from the database table you want to create one model. Remember the model name is the same as the table name in the database that wants to display.
By default, the current page is detected by the value of the page query string argument on the HTTP request. This value is automatically detected by Laravel, and is also automatically inserted into links generated by the paginator.
Implementing a page view counter into your Laravel application seems like the easiest job at first. Just add a column in your database with the current view count and increment that on every page load, right?
Another way, a bit shorter
class AppServiceProvider extends ServiceProvider
{
// other methods
public function boot()
{
$series = Series::all();
view()->share('series_list', $series);
}
// other methods
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With