首页 > 解决方案 > 在 laravel 刀片表中显示数据库中的数据

问题描述

我有一个 HTML 表,我想在其中显示从数据库表中检索到的数据。

我过去用 php 成功地做到了这一点,但使用 Laravel,表中没有显示任何内容:下面是用于在表中显示数据的代码

<tbody>
  @php
 $total_sales = 0;
 $comm_total = 0;
 @endphp

@foreach ($comm as $data){ 
  $comm_total += $data->sales_comm; 
  $total_sales += $data->sales_total;
<tr>
<td>{{$data->sales_date}}</td>
<td>{{$data->sales_total}}</td>
<td>{{$data->sales_comm}}</td>
</tr> 
@endforeach

我希望检索数据库表中具有登录用户 ID 和选定月份和年份的每一行并将其显示在刀片​​表中。

这是控制器代码

$comm = \DB::table('bakerysales')->where('customer_id', Auth()->id()
        ->whereMonth('sales_date',  $request->input('mn'))
        ->whereYear('sales_date', $request->input('yr'))
        ->get();

 return view('showCommission', compact('comm'));

标签: htmllaravel

解决方案


您快到了。2件事:

第一:@foreach ($comm as $data){不需要{

其次,在使用@php 标签时,需要将所有内容都封装在括号内。所以:

@php
$total_sales = 0;
$comm_total = 0;
@endphp

变成:

@php( $total_sales = 0 )
@php( $comm_total = 0 )

总之,它看起来如下所示:

@php( $total_sales = 0 )
@php( $comm_total = 0 ) // Note the omission of the ;

@foreach( $comm as $data )
    <tr>
        <td>{{$data->sales_date}}</td>
        <td>{{$data->sales_total}}</td>
        <td>{{$data->sales_comm}}</td>
    </tr> 
@endforeach 

至于您的控制器代码:

// Make sure you call these at the top of your controller
use Auth;
use App\User;

public function show( Request $request )
{
    $comm = DB::table('bakerysales')
        ->where('customer_id', Auth::id() ) // Getting the Authenticated user id
        ->whereMonth('sales_date', $request->input('mn') )
        ->whereYear('sales_date', $request->input('yr') )
        ->get();

    return view('showCommission', compact('comm') );
}

推荐阅读