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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀