简体   繁体   中英

Issue in fetching data from db to the views in laravel 5.2

Im new to laravel and studying it by creating some test projects myself in laravel 5.2. But i got stuck now with some issue in fetching data correctly from db in laravel 5.2 and display the result. I have a menu table in my db with fields -> id, menutype, itemname, itemprice, itemimage with some datas in it. I want to display it on my webpage in a specific way like as seen on the below given screenshot.

See:

And this one is my db table screenshot with the values on it. See:

i added the below codes in my Controller (GuestController.php)

public function menu() {
    $result=DB::table('menu')->select('menutype')->distinct()->get();
    return view('guest.menu')->with('data',$result);                
}   

and in the View (menu.blade.php), i had given the below code:

<div class="row">
    @foreach($data as $row)
    <div class="col-1-3">
      <div class="wrap-col">
        <h3>{{$row->menutype}}</h3>
        <?php
        $item=DB::table('menu')->where('menutype', $row->menutype)->get();  
        ?>
        @foreach($item as $row)
        <div class="post">
          <a href="#"><img src="assets/images/{{$row->itemimage}}"/></a>
          <div class="wrapper">
            <h5><a href="#">{{$row->itemname}}</a></h5>
            <span>Rs.{{$row->itemprice}}/-</span>
          </div>
        </div>
        @endforeach
      </div>
    </div>
    @endforeach
</div>

This works perfectly and i am getting the desired output as seen on the products page screenshot given above. But i know this method is not correct, because i am giving the query statement on the View itself like as given below to fetch data and its against the MVC concept:

<?php $item=DB::table('menu')->where('menutype', $row->menutype)->get(); ?> 

So is there any other simple and better way i can implement to get the above said desired output along with keeping the MVC Standards?? Please help! Thanks in advance...

Laravel's Collection can really help you out with this. Specifically, the groupBy method. First, you get all of the menu items with all the data. Then, you use the groupBy method on the Collection to group the menu items into separate arrays based on their menutype . You can then use this one Collection to do all the work in your view.

The code is shown below. You can combine a couple of the lines into one if you'd like, but it is broken out into multiple lines to show all the steps:

public function menu() {
    // get all the menu items
    $menuArray = DB::table('menu')->get();
    // create a Laravel Collection for the items
    $menuCollection = collect($menuArray);
    // group the Collection on the menutype field
    $groupedMenu = $menuCollection->groupBy('menutype');

    /**
     * Note that Eloquent queries (using Models) will automatically return
     * Collections, so if you have your Menu model setup, your first two
     * lines would just be:
     * $menuCollection = Menu::get();
     * or, all three lines could be combined into:
     * $groupedMenu = Menu::get()->groupBy('menutype');
     */

    // pass the grouped Collection to the view
    return view('guest.menu')->with('data', $groupedMenu);
}

Now, in your view, your outer foreach will iterate through the groups. The inner foreach will iterate through the items in each group:

<div class="row">
    @foreach($data as $type => $items)
    <div class="col-1-3">
      <div class="wrap-col">
        <h3>{{$type}}</h3>
        @foreach($items as $item)
        <div class="post">
          <a href="#"><img src="assets/images/{{$item->itemimage}}"/></a>
          <div class="wrapper">
            <h5><a href="#">{{$item->itemname}}</a></h5>
            <span>Rs.{{$item->itemprice}}/-</span>
          </div>
        </div>
        @endforeach
      </div>
    </div>
    @endforeach
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM