如何使用Laravel按类别显示产品API

时间:2020-05-01 16:28:55

标签: laravel api

我期望的JSON格式: enter image description here

这是我期望的JSON格式,我有一个产品表以及与产品相关的表。我想按类别显示产品。 产品表: enter image description here

类别表: enter image description here

这是我的产品型号:

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

1 个答案:

答案 0 :(得分:0)

每次加入同一张表时,您应该多次加入

您以适当的别名加入:

$list = Product::
leftJoin('categories as mainCategory','products.category_id','mainCategory.id')
->leftJoin('categories as subCategory','products.sub_category_id','subCategory.id')
->leftJoin('categories as additionalCategory','products.additional_category','additionalCategory.id')
->select(['products.*','mainCategory.name as mainCategoryName','subCategory.name as subCategoryName','additionalCategory.name as additionalCategoryName'])->get();

请注意,您的列“ products.additional_category”应命名为

products.additional_category_id

请参阅: https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html