【发布时间】: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');
}
任何帮助将不胜感激!
【问题讨论】: