2
votes

I have a HTML table in which i want to display data retrieved from a database table.

I have successfully done this with php in the past but with Laravel, nothing is showing in the table: Below is the code for displaying the data in table

<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

I want every row in the database table with the ID of the logged in user and selected month and year to be retrieved and displayed in the blade table.

And here is the controller code

$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'));
1
Use dd() to dump out the value of $comm as a starting point. - ceejayoz

1 Answers

3
votes

You're almost there. 2 things:

First: @foreach ($comm as $data){ doesn't need the {

Second, when using the @php tag, you need to encapsulate everything between brackets. So:

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

Becomes:

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

All together, it looks like the following:

@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 

As for your Controller code:

// 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') );
}