【问题标题】:How To Display Product API by categories using laravel如何使用 laravel 按类别显示产品 API
【发布时间】:2020-08-16 04:23:38
【问题描述】:

我预期的 JSON 格式:

这是我预期的 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 格式相关。请帮助我

标签: laravel api


【解决方案1】:

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

您使用合适的别名加入:

$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://stackoverflow.com/a/21835059/10573560

【讨论】:

  • 感谢您的回复。您所说的我试过了,但我预期的 JSON 格式在这里给出了。请看一下。基本上,我首先想要的我需要检查类别,然后如果类别存在我需要在 categoryDe​​tails 中推送列表然后在这个列表中需要以我预期的 json 格式检查子类别,如果你看那里然后你可以了解我需要什么。请建议我该怎么做
猜你喜欢
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多