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 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单