-1

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 }}">

Then at the Controller I have added this:

if(!empty($request->input('orderCheck'))) {
    $args = [];
    $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)
        ];
    }
    view('admin.shop.orders.printfactors', compact('args'));
}else{
    return back();
}

And finally at the Blade printfactors.blade.php:

@foreach($args as $arg)
    ...
    @forelse($arg['details'] as $detail)
        {{ $detail->product->prd_name }}
    @empty
    @endforelse
@endforeach

But I get this error:

Trying to get property 'product' of non-object (View: printfactors.blade.php)

So how to solve this?

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

Thanks.

Here is the result of dd($detail):

enter image description here

  • 1
    Try dumping $detail so you know if it is an array, and you can find out what you can echo – Gert B. Aug 03 '21 at 06:30
  • @GertB I just added an update on `dd($detail)` and it's not empty –  Aug 03 '21 at 06:32
  • since it is in a loop, it could be null in one of the items. wrap the echo in a if($detail) and maybe add a else to debug – Gert B. Aug 03 '21 at 06:34
  • @GertB. Please would u tell me how to print details info in proper way –  Aug 03 '21 at 06:40
  • The proper way would be starting with one query using whereIn() rather then multiple queries. (just a tip ) the if statement i suggested should fix your problem. – Gert B. Aug 03 '21 at 06:50
  • There are so many duplicates here, even just searching for your error msg shows them: https://stackoverflow.com/questions/23910553/laravel-check-if-related-model-exists, https://stackoverflow.com/questions/51382543/laravel-check-if-relation-is-empty, https://stackoverflow.com/questions/6714591/php-how-to-catch-a-trying-to-get-property-of-non-object-error, https://stackoverflow.com/questions/48118327/laravel-trying-to-get-property-of-non-object-on-first, https://stackoverflow.com/questions/52668889/how-to-solve-trying-to-get-property-of-non-object-in-laravel ... – Don't Panic Aug 03 '21 at 07:11

1 Answers1

0

From your information it looks like $arg['details'] is not coming empty and when fetching relation product in loop, one of the value of $detail is coming as null.

Try debugging for each order, what is the value of $arg['details'].

  • Same result as the picture of `dd($detail)` shows –  Aug 03 '21 at 06:44
  • @KKKKKK try using the [optional function link](https://laravel.com/docs/8.x/helpers#method-optional) to see which value caused the issue. `{{ optional($detail)->product->prd_name }}` – ajay ramgarhia Aug 03 '21 at 06:51
  • I tried `{{ optional($detail)->product->prd_name }}` but still shows the error –  Aug 03 '21 at 06:59
  • @KKKKKK use the nested optional to see if product is coming null as well. `{{optional(optional($detail)->product)->prd_name }}` – ajay ramgarhia Aug 03 '21 at 07:02