Laravel: Trying to access array offset on value of type null

  laravel, php

I am currently working on the selling system, each product will have several documents (PDF, docs, img, excel) and some might not. The documents are grouped in one folder, and the buttons are built for users to download the folder with documents (zip download). The user needs to purchase the product first before download the document folder.

Trying to access array offset on value of type null

The error occurs when I need to access the product detail page with the button for downloading the document folder.

Product Controller

public function specific($id, Product $product, Document $docs)
{
    $product = Product::where('type', 2)->findOrFail($id);

    $document = Document::where('product_id', $product->id)->first(); //

    return view('frontend.directoffer.specific', compact('product', 'document', 'docs'));
}

Below is the checking in order to let the users who had purchased the product, download the document folder.

detail.blade.php

<div>
    // check if product has document
    @if ( count($product->product_document) > 0 ) 
         
    <div>

        <p>You can download the Fact Sheet below by clicking the button below.</p><br>

        @auth

            @php   
                use AppModelsOrder;    
                $orders = Order::where([
                    'member_id' => Auth::user()->profile->member['id'], 
                    'product_id' => $document->product_id
                ])->get();
            @endphp               
            
            // check if user has purchased the product
            @if (count($orders) > 0)

                @foreach($orders as $order)
                <div class="row">
                    @if ($order['order_status'] === 3)

                        {{-- Paid --}}
                        <div><a href="{!! route('web.zip', $product->id) !!}" class="btn-design">
                            Download</a>
                        </div>

                    @else

                        {{-- Unpaid --}}
                        <div><a href="{{ route('web.order', $product->id) }}" class="btn-design">
                            Download</a>
                        </div>

                    @endif
                </div>
                @endforeach

            @else

                // check if user has not purchased the product, will direct to place order page                        
                <div class="row">
                    <div><a href="{{ route('web.order', $product->id) }}" class="btn-design">
                        Download
                    </a></div>
                </div>

            @endif
            
            <div class="row"><small>Note: Please purchase the item before downloading the documents.</small></div>

        @else

            <div class="row">
                <div><a href="#" onclick="openLoginForm()" class="btn-design">Download</a></div>
            </div>

        @endauth

    </div>
    @else

        <div><p>There is no Fact Sheet for this product yet.</p></div>

    @endif
</div>

Product.php

public function product_document()
{
    return $this->hasMany(Document::class, 'product_id', 'id');
}

Document.php

public function product()
{
    return $this->belongsTo(Product::class, 'product_id');
}

Order.php

public function product()
{
   return $this->belongsTo(Product::class, 'product_id');
}

public function member()
{
    return $this->belongsTo(Member::class, 'member_id');
}

Any help would be appreciated!

Source: Laravel

Leave a Reply