Home > Back-end >  Does not show the records in the table Laravel
Does not show the records in the table Laravel

Time:01-25

I have a table where I write the rows and columns but I dont know why any record appear, if the query doesn't say any error.

I dont know where is the error, I try to find how to solve it but I cant

    <tbody>
        @foreach ($salesReport as $saleReport)
            <tr id="{{ $saleReport->id }}">
                <td>{{$saleReport->doctors->name}}</td>
                <td>{{$saleReport->clients->name}}</td>
                <td>{{$saleReport->inventory->lineName}}</td>
                <td>{{$saleReport->vendors->name}}</td>
                <td>{{$saleReport->receipt_dates->name}}</td>
                <td>{{$saleReport->invoice_dates->name}}</td>
                {{-- <td>{{$saleReport->quantity}}</td> --}}
                <td></td>
            </tr>
        @endforeach
    </tbody>
</table>

query:

$salesReport = [];

$salesR = Sale::all();
foreach ($salesR as $saleR) {
    $doctors = Doctor::where('id', $saleR->doctor_id)->first();
    $clients = Client::where('id', $saleR->client_id)->first();
    $vendors = Vendor::where('id', $saleR->vendor_id)->first();
    $receipt_dates = Sale::where('id', $saleR->receipt_date)->first();
    $invoice_dates = Sale::where('id', $saleR->invoice_date)->first();


    $inventory = Inventory::where('sale_id', $saleR->id)
        ->join('products', 'inventories.product_id', '=', 'products.id')
        ->join('line_products', 'products.lineProduct_id', '=', 'line_products.id')
        ->select(
            'inventories.id as inventoryID',
            'inventories.sale_id as salesID',
            'line_products.id as lineID',
            'line_products.name as lineName',
        )
        ->get();
}

enter image description here

CodePudding user response:

We don't see your full code, but I will assume you are correctly sharing the variable with the view on the controller.

Your issue is that you are never storing the data on the variable... But your $salesReport is an array and you are accessing that on the view as an object... So I will just assume you have an stdClass just to comply with your view:

$salesReport = [];

$salesR = Sale::all();
foreach ($salesR as $saleR) {
    $doctors = Doctor::where('id', $saleR->doctor_id)->first();
    $clients = Client::where('id', $saleR->client_id)->first();
    $vendors = Vendor::where('id', $saleR->vendor_id)->first();
    $receipt_dates = Sale::where('id', $saleR->receipt_date)->first();
    $invoice_dates = Sale::where('id', $saleR->invoice_date)->first();


    $inventory = Inventory::where('sale_id', $saleR->id)
        ->join('products', 'inventories.product_id', '=', 'products.id')
        ->join('line_products', 'products.lineProduct_id', '=', 'line_products.id')
        ->select(
            'inventories.id as inventoryID',
            'inventories.sale_id as salesID',
            'line_products.id as lineID',
            'line_products.name as lineName',
        )
        ->get();

    $stdClass = new \stdClass;

    $stdClass->id = $saleR->id;
    $stdClass->doctors = $doctors;
    $stdClass->clients = $clients;
    $stdClass->vendors = $vendors;
    $stdClass->receipt_dates = $receipt_dates;
    $stdClass->invoice_dates = $invoice_dates;

    $salesReport[] = $stdClass;
}

I just did not add quantity as I have no idea where do you get that from.


This is just a solution, not what I would recommend to do at all

  • Related