【问题标题】:Laravel: Trying to access array offset on value of type nullLaravel:尝试访问 null 类型值的数组偏移量
【发布时间】:2021-10-14 03:59:49
【问题描述】:

我目前正在研究销售系统,每个产品都会有几个文档(PDF、docs、img、excel),有些可能没有。文档被分组在一个文件夹中,并且为用户构建了按钮来下载包含文档的文件夹(zip 下载)。用户需要在下载文件夹之前先购买产品

尝试访问 null 类型值的数组偏移量

当我需要使用下载文件夹的按钮访问产品详细信息页面时发生错误。

产品控制器

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'));
}

下面是为了让已购买产品的用户下载文件夹的检查。

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');
}

任何帮助将不胜感激!

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    由于您没有提供堆栈跟踪或为空的特定元素,因此很难判断错误来自何处。但是,我的猜测是这种拉动可能会导致问题:

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

    具体这部分:Auth::user()-&gt;profile-&gt;member['id']

    您的用户可能没有个人资料。在这种情况下它找不到member['id'],因为有一个空值试图调用数组值。

    或者配置文件未加载。尝试在调用数组之前加载它:Auth::user()-&gt;loadMissing('profile');loadMissing 仅加载尚未加载的关系。)

    不知道这些值在用户身上是什么,或者它是如何设置的,这很困难,但同样的问题可能适用于 member 对象——它可能没有设置,也可能无法加载。

    要进行测试,请尝试将用户对象转储到不同级别(仅是用户,然后是个人资料,然后是成员),然后看看会返回什么。这将是一个非常快速的指示空值在哪里。

    【讨论】:

    • 我知道了,Auth::user()-&gt;profile-&gt;member['id'] 为空,因为用户还不是会员。因此,我使用@if (!empty($orders)) insted 计算刀片文件中的订单@if (count($orders) &gt; 0),它不会抛出错误。非常感谢您的宝贵时间!
    猜你喜欢
    • 2021-02-02
    • 2021-01-09
    • 1970-01-01
    • 2021-07-23
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多