title: Dynamic categories in index page with laravel 5
body: <p>I want to give option to admins in order to edit their index page by adding products from different category.</p>
  <h1>Explanation</h1>
  <p>Admin can choose categories from their panel and regarding to that category products in it will show as section in index page.</p>
  <p>admin select <code>category 1</code>, in <code>index</code> we have new section include <code>category 1</code> products. and so on.</p>
  <p>index will be like:</p>
  <blockquote>
  <p>category 1 section <code>10</code> products.</p>
  <p>category 2 section <code>10</code> products.</p>
  <p>category 3 section <code>10</code> products.</p>
  <p>category 4 section <code>10</code> products.</p>
  <p>category 5 section <code>10</code> products.</p>
  </blockquote>
  <p>and admin can add or remove these sections.</p>
  <h1>Code:</h1>
  <p>Currently I only have static section in my index where I get latest products from all categories, i want to have products of each category separately in that section.</p>
  <p>this is my function in <code>AppServiceProvider</code></p>
  <pre><code>View::composer('welcome', function ($view) {
            $productsone = DB::table('products')
            -&gt;latest()
            -&gt;take(10)
            -&gt;get();
            $view-&gt;with('productsone', $productsone);
          });
  </code></pre>
  <p>and i get my data in index page with <code>@foreach($productsone as $product)</code>.</p>
  <p><strong>ps: please share idea with me how i can do that, if i need to create tables etc.</strong></p>
  <h1>update:</h1>
  <p>My issue is not in query, what i want is to know how to build the system to let admin choose <code>unlimited</code> category and pop those category sections in <code>index</code> page. (is really something like <code>WordPress</code> themes <em>some of them</em> that you can select categories and show posts of that in index)</p>
  <h2>sample</h2>
  <p><a href="https://i.stack.imgur.com/BMw2W.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BMw2W.jpg" alt="img1" /></a></p>
  <p>look at those <code>nature</code> and <code>culture</code>. They are category sections in index page.</p>
  <h1>UPDATE 2</h1>
  <p>I've made new table:</p>
  <pre><code>Schema::create('admin_designs', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('category_id')-&gt;unsigned();
              $table-&gt;integer('status_id')-&gt;unsigned();
              $table-&gt;string('title')-&gt;nullable();
              $table-&gt;timestamps();
          });
          Schema::table('admin_designs', function($table) {
             $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories');
             $table-&gt;foreign('status_id')-&gt;references('id')-&gt;on('statuses');
          });
  </code></pre>
  <p>And I've saved some rows in it.</p>
  <p>problem i'm facing during to test this way works or not is that i cannot get my products list. <code>WHY?</code> because my products are not stored under my <code>categories</code> they are stored by <code>subcategories</code>.</p>
  <p>I need kind of query that says:</p>
  <ol>
  <li>look in <code>admin_desings</code> table <code>category_id</code> column</li>
  <li>get that id and match it with <code>subcategories</code> table where the
  subcategories have same <code>id</code> in <code>category_id</code> column</li>
  <li>then match it with <code>products</code> table where they have those
  <code>subcategories</code> id</li>
  </ol>
  <p>in that way i can get products under selected category regardless of what subcategory they are in.</p>
  <p><strong>This is my broken function! :)</strong></p>
  <pre><code>View::composer('welcome', function ($view) {
     $category = Category::with('admin_designs')-&gt;where('id', 'category_id')-&gt;get();
     $subcategory = Subcategory::where('category_id', $category)-&gt;get();
     $designs = AdminDesign::with('products')
        -&gt;where('subcategory_id', $category)
        -&gt;orderby('id', 'desc')
        -&gt;latest()
        -&gt;take(10)
        -&gt;get();
              
  $view-&gt;with('designs', $designs);
  });
  </code></pre>
  <p>Error i'm getting on this query is:</p>
  <blockquote>
  <p>SQLSTATE[HY000]: General error: 2031 (SQL: select * from
  <code>subcategories</code> where <code>category_id</code> = ?)</p>
  </blockquote>