I recently started learning laravel and I'm building a little project to try it out 'real world'.
So, I have a view split with some @includes
e.g. main.blade.php which includes sidenav.blade.php, header.php etc..
I have created a "Product" model + controller and DB migration and added a few products in the database.
My understanding was that I would then be able to have a function in the Product model that queries the DB and returns all products which I can then use anywhere on the site e.g. On products page I might want to list all products, or in my current case i want to list all product names in the sidenav.blade.php file.
This is where I am running into trouble. The closest I got was to:
- @yield('productlist') in sidenav
- create a new view called 'products/index.blade.php' and in there extend sidenav.blade.php
- put the foreach product loop in there as @section('productlist')
- Create a Route::get('product', 'ProductController@index') where I see the complete combined files all working as expected
But when i visit a normal page (which is including sidenav) it doesn't recognise the products variable
Is this just completely the wrong way to do this sort of thing? Or am I actually getting close?