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

alexray's avatar

Problem in displaying data!

I want to display orders list according to their order type but it only displays the orders of one out of three order types.

here is my code:

controller:

function index()
{
    $ordertypes = DB::table('ordertypes')
        ->select('ordertypes.*')
        ->get();



    $order = DB::table('orders')
        ->join('tables', 'tables.id', '=', 'orders.table_id')
        ->join('deliveries', 'deliveries.id', '=', 'orders.table_id')
        ->join('ordertypes', 'ordertypes.id', '=', 'orders.ordertype_id')
        ->join('menus', 'menus.id', '=', 'orders.menu_id')
        ->join('users', 'users.id', '=', 'orders.user_id')
        ->select('orders.*', 'tables.table_number AS table', 'tables.floor AS floor', 'deliveries.name AS customer_name', 'deliveries.id AS delivery_id', 'deliveries.address AS customer_address', 'deliveries.phone AS customer_phone', 'deliveries.delivery_time AS deliverytime', 'ordertypes.type_name AS ordertype','menus.food_name AS food_name', 'menus.size AS food_size', 'users.name AS username')
        ->get();

    return view('orders.order', ['order' => $order, 'ordertypes' => $ordertypes]);
}

blade:

        <div x-show="currentTab === 'second'" class="rounded-md p-6 mx-auto" x-data="{currentTabSecond: 'dine in'}">
          <div class="flex gap-2">
            @foreach ($ordertypes as $ordertype)
                <input class="p-2 cursor-pointer rounded bg-slate-400"  id="{{$ordertype->id}}" type="button" name="{{$ordertype->type_name}}" value="{{$ordertype->type_name}}"  @click="currentTabSecond = '{{(strtolower($ordertype->type_name))}}'">
            @endforeach
          </div>
          <!-- Order Lists -->
          <table class="mx-auto border-slate-200 border rounded-lg overflow-hidden" x-show="currentTabSecond === 'dine in' || currentTabSecond === 'delivery' || currentTabSecond === 'pick up'">
            <thead>
              <tr>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">ID</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">Food Name</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">Size</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">Order Type</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">Notes</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">Quantity</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600" x-show="currentTabSecond === 'dine in'">Table</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600" x-show="currentTabSecond === 'delivery'">Delivery Time</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600" x-show="currentTabSecond === 'pick up'">Pickup Time</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">Status</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600">User Name</th>
                <th class="px-6 py-3 bg-slate-300 border-b-2 border-slate-300 text-gray-600" colspan="2">Action</th>
              </tr>
            </thead>
            <tbody >
              @foreach ($order as $order)
              @if ((strtolower($order->ordertype) == "dine in"))
              <tr class="{{ $loop->even ? 'bg-gray-50' : 'bg-white' }}" x-show="currentTabSecond === 'dine in'">
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->id }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->food_name }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->food_size }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->ordertype }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->notes }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->quantity }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->table }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->status }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600 text-center">{{ $order->username }}</td>
                <!-- action -->
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300">
                  <a href="/order/{{ $order->id }}/edit" class="text-blue-500 hover:underline">Edit</a>
                </td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300">
                  <a href="#" onclick="confirmDelete('{{ $order->id}}','null')" class="text-red-500 hover:underline">Cancel</a>
                </td>
              </tr>
              @elseif ((strtolower($order->ordertype) == "delivery"))
              <tr class="{{ $loop->even ? 'bg-gray-50' : 'bg-white' }}" x-show="currentTabSecond === 'delivery'">
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->id }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->food_name }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->food_size }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->ordertype }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->notes }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->quantity }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->deliverytime }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->status }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600 text-center">{{ $order->username }}</td>
                <!-- action -->
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300">
                  <a href="/order/{{ $order->id }}/edit" class="text-blue-500 hover:underline">Edit</a>
                </td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300">
                  <a href="#" onclick="confirmDelete('{{ $order->id}}','{{ $order->delivery_id}}')" class="text-red-500 hover:underline">Cancel</a>
                </td>
              </tr>
              @elseif ((strtolower($order->ordertype) == "pick up"))
              <tr class="{{ $loop->even ? 'bg-gray-50' : 'bg-white' }}" x-show="currentTabSecond === 'pick up'">
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->id }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->food_name }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->food_size }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->ordertype }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->notes }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->quantity }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->deliverytime }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600">{{ $order->status }}</td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300 capitalize text-gray-600 text-center">{{ $order->username }}</td>
                <!-- action -->
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300">
                  <a href="/order/{{ $order->id }}/edit" class="text-blue-500 hover:underline">Edit</a>
                </td>
                <td class="px-6 py-4 bg-slate-100 border-b border-gray-300">
                  <a href="#" onclick="confirmDelete('{{ $order->id}}', '{{ $order->delivery_id}}')" class="text-red-500 hover:underline">Cancel</a>
                </td>
              </tr>
              @else
              <h1>Undefined ordertype!</h1>
              @endif
              @endforeach
            </tbody>
          </table>
        </div>
0 likes
7 replies
Snapey's avatar
Snapey
Best Answer
Level 122

you destroy $order here

@foreach ($order as $order)

suggest you get plural $orders in the controller and change the foreach

@foreach ($orders as $order)
alexray's avatar

@Snapey plural $orders in the controller and change the foreach Can the problem be from migrations etc?

Snapey's avatar

@alexray inspect the data that you pass to the view. Does it contain each order type?

alexray's avatar

I did php artisan migrate:refresh --path=/database/migrations/2024_02_03_113425_create_orders_table.php and now it displays none of the data. I used dd(['orders' => $orders]); and the result was:

array:1 [▼ // app\Http\Controllers\OrderController.php:38 "orders" => Illuminate\Support\Collection {#1356 ▼ #items: [] #escapeWhenCastingToString: false } ]

Please or to participate in this conversation.