Amine Bouhaddi
Amine Bouhaddi

Reputation: 584

best way to query my database using laravel

I started working on a small project using Laravel, so i have three tables in my database

  1. Products
  2. Categories
  3. Brands

i want to list all my product in a HTML table, right now i use in my controller this method leftJoin is that the correct way or i have to do that with eloquent if yes how give me an example

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

This is my result :

"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": "[email protected]",
        "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
    }
  ]

Upvotes: 0

Views: 60

Answers (1)

TsaiKoga
TsaiKoga

Reputation: 13404

Try to use Eloquent one-to-many will be elegant:

In your Category Model

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

In your Brand Model:

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

In your Supplier Model:

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

In your Product Model:

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

So you can query just like this:

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

Upvotes: 2

Related Questions