Skip to content
Advertisement

How to send multiple data into view with their required relationships

I am working on a Laravel 5.8 project which is an Online Store. and in this project, I wanted to add “Printing Order Factors” feature for Admins.

So I have made a form like this:

<form method="POST" action="{{ route('orders.newprint') }}">
    @csrf
    @forelse($orders as $order)
    <tr>
        <td><input class="form-check-input" name="orderCheck[]" type="checkbox" value="{{ $order->ord_id }}">&nbsp;</td>
        <td>{{ $order->ord_id }}</td>
        <td>{{ $order->status_label }}</td>
        <td>{{ $order->customer_name }}</td>
        <td>{{ $order->ord_total }}</td>
    </tr>
    @empty
        <td colspan="7" class="text-center">No order to show</td>
    @endforelse
</form>

So basically admins can select multiple orders like this:

enter image description here

And I’m trying to send order ids as an array:

<input class="form-check-input" name="orderCheck[]" type="checkbox" value="{{ $order->ord_id }}">

So after clicking Print All Order Factors, it should be showing all the order factors at one page.

So in order to do that, I have added this method at the Controller:

public function prnpriview(Request $request)
    {
        $orders = $request->input('orderCheck');
        foreach($orders as $ord){
            $order = Order::find($ord);
            $args = [
                'order'  => $order,
                'address'  => $order->order_address->first(),
                'details'  => $order->orderDetail,
                'sendType'  => $order->productSubmit,
                'coupons'  => $this->cartController->computeDiscountForOutRequests($order->ord_object_id, $order->ord_creator_id),
                'user'      => User::query()->find($order->ord_creator_id)
            ];

        }
        return view('admin.shop.orders.newprint', $args);
    }

However the problem with this is that, it only sends the last order and not all of them.

The controller code used to work fine for one order. But I need to pass multiple orders to newprint.blade.php.

So the question is, how to send all of the orders that have been checked by user and send them to view with their args array (required relationships)?

I would really appreciate any idea or suggestion from you guys about this…

Thanks.


UPDATE #1

Here is my code at newprint.blade.php which shows every order factor details for printing:

@foreach($args as $arg)
    <p>Order Id:
        <strong>
            {{ digits2persian($order->ord_id) }}
        </strong>
    </p>
    <p>Address:
        <strong>
            {{ $address->province_relation->prv_name }}،
            {{ $address->city_relation->cit_name }}،
            {{ $address->address }}
        </strong>
    </p>
    ...
@endforeach 

UPDATE #2:

If I dd($args) before return in the Controller, and if I {{ dd($args) }} in the view, I get this as result:

enter image description here

Advertisement

Answer

You’re close 🙂 You need to create an array of arrays using $arg. Right now, in the loop, it’s replacing the value of the array each time it loops through it.

$args = [];
foreach($orders as $ord){
    $args[] = [
        ...
    ];
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement