简体   繁体   English

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

[英]How to display all row in database - 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: 我的Laravel应用程序有一个简单的问题,但找不到任何解决方案,请在此处查看我的代码:

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 . 并在表中: 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? 如果数据库中有row ,则可以查看请参阅 )。但是如果row多于一行,则其所有数据都显示在row请参阅 )。我该如何处理呢? I think it should be run another loop but how can i do that? 我认为应该运行另一个循环,但是我该怎么做呢? Thanks in advance. 提前致谢。

So, if you're using Laravel, one of the best features that has is the accessors: 因此,如果您使用的是Laravel,则具有的最佳功能之一就是访问器:

https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators 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! 请享用! :) :)

A quick patch would be to do only one query and then do the calculations on the view (I know that is not a good practice to do the calculations in the view, but is the easiest way that i can see). 一个快速的修补程序是只执行一个查询,然后在视图上进行计算(我知道这不是在视图中进行计算的好习惯,但这是我能看到的最简单的方法)。

It would be like this: 就像这样:

Controller: 控制器:

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

    return view('member.mealView', compact('members'));
}

blade view 刀片视图

<tbody>
    @foreach($members as $meal_entry)
    <tr>
        <td>{{ $meal_entry->created_at }}</td>

        <td>{{ $breakfast = $meal_entry->self_meal_breakfast + $meal_entry->guest_meal_breakfast }}</td>

        <td>{{ $lunch = $meal_entry->self_meal_lunch+ $meal_entry->guest_meal_lunch}}</td>

        <td>{{ $dinner = $meal_entry->self_meal_dinner + $meal_entry->guest_meal_dinner }}</td>

        <td>{{ $breakfast + $lunch + $dinner }}</td>

        <td>{{ $meal_entry->created_at }}</td>
    </tr>
    @endforeach 
</tbody>

And remember to add 并记得添加

{{ $members->links() }}

Where you want the pagination links. 您想要分页链接的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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