【问题标题】:Displaying data from database in laravel blade table在 laravel 刀片表中显示数据库中的数据
【发布时间】:2019-04-08 15:43:27
【问题描述】:

我有一个 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'));

【问题讨论】:

  • 使用dd() 导出$comm 的值作为起点。

标签: html laravel


【解决方案1】:

你快到了。两件事:

首先:@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') );
}

【讨论】:

    猜你喜欢
    • 2022-08-14
    • 1970-01-01
    • 2020-02-03
    • 2020-08-19
    • 2021-01-07
    • 2023-01-08
    • 2020-07-26
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多