xyz
xyz

Reputation: 91

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

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)
{
    $product = Product::where('type', 2)->findOrFail($id);

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

    return view('web.detail', compact('product', 'document'));
}

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 App\Models\Order;    
                $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.download', $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

                ...

        @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!

Upvotes: 0

Views: 7135

Answers (1)

Watercayman
Watercayman

Reputation: 8178

Its hard to tell where the error is coming from as you didn't supply a stack trace or the specific element that was null. But, my guess is that this pull might be causing the problem:

$orders = Order::where([
      'member_id' => Auth::user()->profile->member['id'], 
      'product_id' => $document->product_id
])->get();

Specifically this part: Auth::user()->profile->member['id']

It is possible that your user does not have a profile. In which case it can't find member['id'] because there is a null value trying to call the array value.

Or perhaps the profile was not loaded. Try loading it before the call for the array: Auth::user()->loadMissing('profile'); (loadMissing just loads the relationship if it wasn't already loaded.)

Not knowing what those values are on the user, or how it is set up makes it hard, but the same issues might apply to the member object -- it may not be set, or may not load.

To test, try dumping the user object at different levels (just user, then with profile, then with member) and see what comes back. This will be a pretty quick indicator of where the null value is.

Upvotes: 2

Related Questions