在开发电子商务网站时,我的商店页面上有类别侧边栏,并且在实际服务器和本地服务器上一切正常。然后,我想添加一个品牌部分,其概念与类别部分相同。因此,每种产品都可以属于一个或多个类别以及一个或多个品牌。
添加品牌部分后,本地服务器上的所有内容也都运行顺利,但是当我要进行部署时,出现以下内存不足错误:
错误:
PHP Fatal error: Out of memory (allocated 3527409664) (tried to allocate 20480 bytes) in /app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php on line 50
以下是我的相关文件和代码,如果需要,我将很乐意提供更多代码。希望有人可以帮助解决它。谢谢。
ShopController.php:
public function index()
{
$pagination = 9;
$categories = Category::all();
$brands = Brand::all();
if (request()->category) {
$products = Product::with('categories')->whereHas('categories', function ($query) {
$query->where('slug', request()->category);
});
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
$brandName = $categoryName;
} else
if (request()->brand) {
$products = Product::with('brands')->whereHas('brands', function ($query) {
$query->where('slug', request()->brand);
});
$brandName = optional($brands->where('slug', request()->brand)->first())->name;
$categoryName = $brandName;
} else {
$products = Product::where('featured', true);
$brandName = 'Featured';
$categoryName = 'Featured';
}
if (request()->sort == 'low_high') {
$products = $products->orderBy('price')->paginate($pagination);
} elseif (request()->sort == 'high_low') {
$products = $products->orderBy('price', 'desc')->paginate($pagination);
} else {
$products = $products->paginate($pagination);
}
return view('shop')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
'brands' => $brands,
'brandName' => $brandName,
]);
}
Brand.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Brand extends Model
{
protected $table = 'brand';
public function products()
{
return $this->belongsToMany('App\Product');
}
}
BrandProduct.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BrandProduct extends Model
{
protected $table = 'brand_product';
protected $fillable = ['product_id', 'brand_id'];
}