簡體   English   中英

使用 Laravel 查詢我的數據庫的最佳方法

[英]best way to query my database using laravel

我開始使用 Laravel 做一個小項目,所以我的數據庫中有三個表

  1. 產品
  2. 類別
  3. 品牌

我想在 HTML 表格中列出我的所有產品,現在我在我的控制器中使用這個方法leftJoin是正確的方法,或者我必須用 eloquent 如果是的話如何給我一個例子

public function index()
{
    $query = DB::table('products')
    ->leftJoin('categories','categories.id', '=', 'products.product_category')
    ->leftJoin('brands','brands.id', '=', 'products.product_brand')
    ->leftJoin('suppliers','suppliers.id', '=', 'products.product_supplier')
    ->orderBy('products.id', 'desc')->paginate(15);
    return response()->json($query);
}

這是我的結果:

"data": [
    {
        "id": null,
        "product_name": "Dylan Hernandez",
        "product_sell_as": "Delectus pariatur",
        "product_upc": 12541,
        "product_sku": "12541",
        "product_description": "Et enim quisquam ani",
        "product_category": 2,
        "product_supplier": 2,
        "product_buying_price": "390.00",
        "product_selling_price": "226.00",
        "product_min_stock": "10.00",
        "product_max_stock": "10.00",
        "product_alert_stock": 10,
        "product_alert_phone": "+1 (955) 289-5975",
        "product_alert_email": "mexi@mailinator.net",
        "product_cost_price": "145.00",
        "product_supplier_discount": "10.00",
        "product_uom": 2,
        "product_brand": 2,
        "product_warehouse": 2,
        "product_availability": 2,
        "product_image": "public/product/tDlkQkCZA4aWDnFkWIed8TKQEP0Wgu0zYGtYfD5T.jpeg",
        "deleted_at": null,
        "created_at": null,
        "updated_at": null,
        "category_name": "Shoes",
        "brand_name": "Dolce Gabana",
        "supplier_company": null,
        "supplier_code": null,
        "supplier_tax_number": null,
        "supplier_phone": null,
        "supplier_fax": null,
        "supplier_website": null,
        "supplier_email": null,
        "supplier_note": null,
        "supplier_type": null,
        "supplier_address": null,
        "supplier_city": null,
        "supplier_state": null,
        "supplier_zip": null,
        "supplier_country": null,
        "supplier_representative": null,
        "supplier_price_list": null,
        "supplier_tax_type": null,
        "supplier_currency": null
    }
  ]

嘗試使用 Eloquent一對多會很優雅:

在您的類別模型中

        public function products()
        {
            return $this->hasMany(\App\Product::class, 'product_category', 'id');
        }

在您的品牌模型中:

        public function products()
        {
            return $this->hasMany(\App\Brand::class, 'brand', 'id');
        }

在您的供應商模型中:

        public function products()
        {
            return $this->hasMany(\App\Supplier::class, 'product_supplier', 'id');
        }

在您的產品型號中:

        public function category()
        {
            return $this->belongsTo(\App\Category::class, 'product_category');
        }

        public function brand()
        {
            return $this->belongsTo(\App\Brand::class, 'brand');
        }  

        public function supplier()
        {
            return $this->belongsTo(\App\Supplier::class, 'product_supplier');
        }

所以你可以像這樣查詢:

Product::with(['category', 'brand', 'supplier'])->orderBy('id', 'desc')->paginate(15);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM