doutangguali32556 2016-02-11 21:43
浏览 66
已采纳

将数据从db获取到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...

  • 写回答

1条回答 默认 最新

  • duanjia4969 2016-02-12 01:15
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划