【发布时间】:2020-08-16 04:23:38
【问题描述】:
这是我预期的 JSON 格式,我有一个产品表以及产品相关表。我想明智地按类别展示产品。 产品表:
这是我的产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
private $minimum_price;
/**
* Get the products image.
*
* @return string
*/
public function getImageUrlAttribute()
{
if (!empty($this->image)) {
$image_url = asset('/storage/img/product/' . $this->image);
} else {
$image_url = asset('/img/default.png');
}
return $image_url;
}
public function product_variations()
{
return $this->hasMany(\App\ProductVariation::class);
}
/**
* Get the brand associated with the product.
*/
public function brand()
{
return $this->belongsTo(\App\Brands::class);
}
/**
* Get the unit associated with the product.
*/
public function unit()
{
return $this->belongsTo(\App\Unit::class);
}
/**
* Get category associated with the product.
*/
public function category()
{
return $this->belongsTo(\App\Category::class);
}
/**
* Get sub-category associated with the product.
*/
public function sub_category()
{
return $this->belongsTo(\App\Category::class, 'sub_category_id', 'id');
}
/**
* Get the brand associated with the product.
*/
public function product_tax()
{
return $this->belongsTo(\App\TaxRate::class, 'tax', 'id');
}
/**
* Get the variations associated with the product.
*/
public function variations()
{
return $this->hasMany(\App\Variation::class);
}
/**
* If product type is modifier get products associated with it.
*/
public function modifier_products()
{
return $this->belongsToMany(\App\Product::class, 'res_product_modifier_sets', 'modifier_set_id', 'product_id');
}
/**
* If product type is modifier get products associated with it.
*/
public function modifier_sets()
{
return $this->belongsToMany(\App\Product::class, 'res_product_modifier_sets', 'product_id', 'modifier_set_id');
}
/**
* Get the purchases associated with the product.
*/
public function purchase_lines()
{
return $this->hasMany(\App\PurchaseLine::class);
}
public function images()
{
return $this->hasMany(ProductImage::class);
}
public function attributes()
{
return $this->hasMany(ProductAttribute::class);
}
public function attributesWithoutDefault()
{
return $this->hasMany(ProductAttribute::class)->where('attribute_id', '>', 4);
}
public function vendor_product()
{
return $this->hasMany(VendorProduct::class, 'product_id', 'id');
}
public function origin()
{
return $this->belongsTo(ProductOrigin::class);
}
public function defaultAttributes()
{
return $this->hasMany(ProductAttribute::class)->where('attribute_id', '<=', 4);
}
public function notes()
{
return $this->hasMany(ProductNote::class);
}
public function additionalCategory()
{
return $this->belongsTo(\App\Category::class, 'additional_category', 'id');
}
public function getAdditionalCategoryNameAttribute()
{
$additional_category = $this->additionalCategory;
return $additional_category ? $additional_category->name : null;
}
public function industries()
{
return $this->belongsToMany(Industry::class, 'product_industries');
}
public function getCatalogUrlAttribute()
{
return asset('/uploads/'. constants('product_catalog_path') . '/' . $this->catalog_brusher);
}
public function getSpecSheetUrlAttribute()
{
return asset('/uploads/'. constants('product_spec_sheet_path') . '/' . $this->spec_sheet);
}
public function relatedProducts()
{
return $this->belongsToMany(Product::class, 'related_products', 'product_id', 'related_product_id');
}
private function setMinPrice()
{
if (is_null($this->minimum_price)) {
$this->minimum_price = $this->vendor_product->min('price');
}
return $this->minimum_price;
}
public function getMinPriceAttribute()
{
return $this->setMinPrice();
}
public function getMinPriceVendorAttribute()
{
$min_price = $this->setMinPrice();
if (is_null($min_price)) return null;
$min_vendor_product = $this->vendor_product->firstWhere('price', $min_price);
return Contact::find($min_vendor_product->vendor_id);
}
/**
* Get the unit quantity associated with the product.
*/
public function unitQuantity()
{
return $this->belongsTo(\App\UnitQuantity::class);
}
}
【问题讨论】:
-
你已经建立好关系了吗?
-
我建立了我的关系,这是我的产品型号: public function category() { return $this->belongsTo(\App\Category::class); } /** * 获取与产品关联的子类别。 */ public function sub_category() { return $this->belongsTo(\App\Category::class, 'sub_category_id', 'id'); }
-
请将代码添加到您的问题中,而不是在评论中
-
@ChristopheHubert 我在这个问题上附上了我的产品模型,如果您需要更多,请提及我
-
@ChristopheHubert 你好兄弟,基本上我想从我的产品表中按类别展示我的产品。出于这个原因,我需要加入类别表,但是在这种情况下检索数据的过程是什么,这与三个或更少的类别以及我预期的 JSON 格式相关。请帮助我