Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

scala's avatar
Level 1

display data in table format from database

I am trying to display data from database to table format. But only table is displayed but not the data. Please see the image. Table is ordered_books with attributes 'BookID', 'BilledNum','BilledDate', 'Qunatity', 'Price', 'Remarks' My questions are ::

  1. Why is the data not populating in the table?
  2. How can I check whether the table data from database is retrieved in Controller $ordered_books variable? view page code
<table id="showBooksIn" class="table table-bordered">
            <thead>
                <tr>
                    <th>BOOK ID</th>
                    <th>BILLED DATE</th>
                    <th>BILLED NUMBER</th>
                    <th>QUANTITY</th>
                    <th>PRICE</th>
                    <th>REMARKS</th>
                </tr>
            </thead>
            @foreach($data as $row)
            <tr>
                <td>{{$row['BookID']}}</td>
                <td>{{$row['BilledNum']}}</td>
                <td>{{$row['BilledDate']}}</td>
                <td>{{$row['Qunatity']}}</td>
                <td>{{$row['Price']}}</td>
                <td>{{$row['Remarks']}}</td>
            </tr>
            @endforeach
        </table>

controller code

public function index()
    {
        return view('pages.booksin', $this->fetchData());
    }

    function fetchData()
    {
        $ordered_books = OrderedBook::all()->toArray();
        return compact('ordered_books');
    }

model code

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderedBook extends Model
{
    //
}

route code

Route::resource('/book','BookController');
Route::resource('/order','OrderedBookController');
Route::get('/order/data','OrderedBookController@fetchData')->name('data');

Please help!!!

url of image uploaded :: https://ibb.co/mCc5od

0 likes
3 replies
m7vm7v's avatar

In your view why do you loop the $data when you are sending $ordered_books from the controller.

For debugging you could use Xdebug but I would recommend you just dd();

in your controller@index

$ordered_books = OrderedBook::all();

dd($ordered_books);

return view('pages.booksin', compact('ordered_books'));

in your blade {{ $row->BookID }}

scala's avatar
Level 1

@m7vm7v getting error when updated the code {{ $row->BookID }} Trying to get property 'BookID' of non-object

m7vm7v's avatar

@scala Have you changed the controller and the loop in the view as I've mentioned. Make sure you have removed ->toArray() in your controller. Its not necessary but you do not need to cast it to array as you loose a lot of other functionality.

in your controller

public function index()
{
        return view('pages.booksin', [
        'ordered_books' => OrderedBook::all()
    ]);
}

in the view

    @foreach($ordered_books as $book)
            <tr>
                <td>{{ $book->BookID }}</td>
        ....
            </tr>
    @endforeach

Please or to participate in this conversation.