duanmibei1929 2016-11-16 08:37
浏览 28
已采纳

如何显示数据库中的所有行 - Laravel 5.2?

I have simple issue with my Laravel application.But i can not find any solution of it.Please see my code here:

controller:

public function index(){
    $members = Member::paginate(15);

    $meal_entry = Member::all();
    $breakfast = DB::SELECT("SELECT self_meal_breakfast + guest_meal_breakfast AS breakfast FROM meal_entry");
    $lunch = DB::SELECT("SELECT self_meal_lunch + guest_meal_lunch AS lunch FROM meal_entry");        
    $dinner = DB::SELECT("SELECT self_meal_dinner + guest_meal_dinner AS dinner FROM meal_entry");
    $total_meal = DB::SELECT("SELECT self_meal_breakfast + self_meal_lunch + self_meal_dinner + guest_meal_breakfast + guest_meal_lunch + guest_meal_dinner AS total FROM meal_entry");

    return view('member.mealView', compact('members','data','breakfast','lunch','dinner','meal_entry','total_meal'));
}

blade view:

<tbody>
    <tr>
        @foreach($meal_entry as $value)
        <td>{{ $value->created_at }}</td>
        @endforeach                                        
        @foreach($breakfast as $value)
        <td>{{ $value->breakfast }}</td>
        @endforeach                                        
        @foreach($lunch as $value)
        <td>{{ $value->lunch }}</td>
        @endforeach                                        
        @foreach($dinner as $value)
        <td>{{ $value->dinner }}</td>
        @endforeach                                        
        @foreach($total_meal as $value)
        <td>{{ $value->total }}</td>
        @endforeach                                        
        @foreach($meal_entry as $value)
        <td>{{ $value->created_at }}</td>
        @endforeach
    </tr>
</tbody>

And the table: meal_entry.

If there is a one row in the database then its ok ( see ) to view.But when there is row more than one then its show in one row with all data( see ).How can i handle this things? I think it should be run another loop but how can i do that? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dougai2427 2016-11-16 09:17
    关注

    So, if you're using Laravel, one of the best features that has is the accessors:

    https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators

    You can create new attributes and use them as model attributes.

    For some stuff are very useful.

    class Meal extends Model
    {
        public function getTotalBreakfastAttribute()
        {
            return $this->self_meal_breakfast + $this->guest_meal_breakfast;
        }
    }
    

    And then, use this accessor as attribute:

    <tbody>
       @foreach($foods as $value)
        <tr>
           <td> {{$value->total_breakfast}} </td> 
           <td> {{$value->created_at}} </td>
        </tr>
      @endforeach
    </tbody>
    

    Or if you don't want to do that, instead of use multiple queries to get the data, just use one because all the fields are in the same table, and then, you could use the above example.

    Enjoy! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?