简体   繁体   中英

Select and count using Laravel Eloquent

I'm working on a small project using Laravel, right now i display my products that i have in my database in HTML table.

So right now i have created another database table called variants, to store products variants ,

i would like to count the number of variants that i have for each product this is my code : as you can see i select also the categories, brands, suppliers i want to count the number of the variants ( since i have already a table called variants , contain the product_id field )

public function index()
{
    $products = Product::with(['category', 'brand', 'supplier'])->orderBy('id', 'desc')->paginate(15);
    return response()->json($products);
}

My current Result :

{
"current_page": 1,
"data": [
    {
        "id": 3,
        "product_name": "Ipad",
        "product_description": null,
        "product_category": 4,
        "product_supplier": 1,
        "product_buying_price": "5500.00",
        "product_selling_price": "66.00",
        "product_min_stock": "10.00",
        "product_max_stock": "10.00",
        "product_alert_stock": 10,
        "product_alert_phone": "+1 (1084) 565-43109",
        "product_alert_email": "daqaxipig@mailinator.net",
        "product_cost_price": "65.00",
        "product_supplier_discount": "10.00",
        "product_brand": 5,
        "product_warehouse": 2,
        "product_availability": 2,
        "deleted_at": null,
        "created_at": null,
        "updated_at": null,
        "category": {
            "id": 4,
            "category_name": "TABLET",
            "category_parent": null,
            "category_description": null,
            "deleted_at": null,
            "created_at": null,
            "updated_at": null
        },
        "brand": {
            "id": 5,
            "brand_name": "Apple",
            "deleted_at": null,
            "created_at": null,
            "updated_at": null
        },
        "supplier": {
            "id": 1,
            "supplier_company": "Dataxpress",
            "supplier_code": "55598548455",
            "supplier_tax_number": "68958955",
            "supplier_phone": "4382251059",
            "supplier_fax": "4382251059",
            "supplier_website": "oppo.com",
            "supplier_email": "ab@dothostia.com",
            "supplier_note": "Aliquip porro est n",
            "supplier_type": 1,
            "supplier_address": "Consequuntur maxime",
            "supplier_city": "Qui consectetur non",
            "supplier_state": "Ex nemo sit quia ame",
            "supplier_zip": "46033",
            "supplier_country": "HU",
            "supplier_representative": 2,
            "supplier_price_list": 4,
            "supplier_tax_type": 2,
            "supplier_currency": "GBP",
            "deleted_at": null,
            "created_at": "2020-01-17 18:16:55",
            "updated_at": "2020-01-17 18:16:55"
        }
    },
    {
        "id": 2,
        "product_name": "Samsung A35",
        "product_description": "test",
        "product_category": 1,
        "product_supplier": 1,
        "product_buying_price": "500.00",
        "product_selling_price": "500.00",
        "product_min_stock": "10.00",
        "product_max_stock": "20.00",
        "product_alert_stock": 15,
        "product_alert_phone": "+1 (1023) 803-2526",
        "product_alert_email": "duvupihujo@mailinator.net",
        "product_cost_price": "600.00",
        "product_supplier_discount": "30.00",
        "product_brand": 4,
        "product_warehouse": 2,
        "product_availability": 2,
        "deleted_at": null,
        "created_at": null,
        "updated_at": null,
        "category": {
            "id": 1,
            "category_name": "Super Phone",
            "category_parent": null,
            "category_description": null,
            "deleted_at": null,
            "created_at": null,
            "updated_at": null
        },
        "brand": {
            "id": 4,
            "brand_name": "Samsung",
            "deleted_at": null,
            "created_at": null,
            "updated_at": null
        },
        "supplier": {
            "id": 1,
            "supplier_company": "Dataxpress",
            "supplier_code": "55598548455",
            "supplier_tax_number": "68958955",
            "supplier_phone": "4382251059",
            "supplier_fax": "4382251059",
            "supplier_website": "oppo.com",
            "supplier_email": "ab@dothostia.com",
            "supplier_note": "Aliquip porro est n",
            "supplier_type": 1,
            "supplier_address": "Consequuntur maxime",
            "supplier_city": "Qui consectetur non",
            "supplier_state": "Ex nemo sit quia ame",
            "supplier_zip": "46033",
            "supplier_country": "HU",
            "supplier_representative": 2,
            "supplier_price_list": 4,
            "supplier_tax_type": 2,
            "supplier_currency": "GBP",
            "deleted_at": null,
            "created_at": "2020-01-17 18:16:55",
            "updated_at": "2020-01-17 18:16:55"
        }
    },
    {
        "id": 1,
        "product_name": "Elton Mullins",
        "product_description": "Possimus rerum comm",
        "product_category": 2,
        "product_supplier": 1,
        "product_buying_price": "97.00",
        "product_selling_price": "354.00",
        "product_min_stock": "10.00",
        "product_max_stock": "20.00",
        "product_alert_stock": 5,
        "product_alert_phone": "+1 (884) 587-2414",
        "product_alert_email": "pywu@mailinator.com",
        "product_cost_price": "989.00",
        "product_supplier_discount": "10.00",
        "product_brand": 3,
        "product_warehouse": 2,
        "product_availability": 2,
        "deleted_at": null,
        "created_at": null,
        "updated_at": null,
        "category": {
            "id": 2,
            "category_name": "Phones",
            "category_parent": null,
            "category_description": null,
            "deleted_at": null,
            "created_at": null,
            "updated_at": null
        },
        "brand": {
            "id": 3,
            "brand_name": "Oppo",
            "deleted_at": null,
            "created_at": null,
            "updated_at": null
        },
        "supplier": {
            "id": 1,
            "supplier_company": "Dataxpress",
            "supplier_code": "55598548455",
            "supplier_tax_number": "68958955",
            "supplier_phone": "4382251059",
            "supplier_fax": "4382251059",
            "supplier_website": "oppo.com",
            "supplier_email": "ab@dothostia.com",
            "supplier_note": "Aliquip porro est n",
            "supplier_type": 1,
            "supplier_address": "Consequuntur maxime",
            "supplier_city": "Qui consectetur non",
            "supplier_state": "Ex nemo sit quia ame",
            "supplier_zip": "46033",
            "supplier_country": "HU",
            "supplier_representative": 2,
            "supplier_price_list": 4,
            "supplier_tax_type": 2,
            "supplier_currency": "GBP",
            "deleted_at": null,
            "created_at": "2020-01-17 18:16:55",
            "updated_at": "2020-01-17 18:16:55"
        }
    }
],
"first_page_url": "http://app.quicknsales.com/backend/product?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://app.quicknsales.com/backend/product?page=1",
"next_page_url": null,
"path": "http://app.quicknsales.com/backend/product",
"per_page": 15,
"prev_page_url": null,
"to": 3,
"total": 3
}

i need to add a key for ex : nbr_variants : x, foreach data item

I assumed your product table hasMany relation with variants . As you said in comment products.id & variants.product_id

Make function variants in product model using hasMany relationship.

Class Product extends Model{
    public function variants(){
        return $this->hasMany('App\Variant','product_id','id');
    }
}

And now you can call it by using withCount

public function index()
{
    $products = Product::with(['category', 'brand', 'supplier','variants'])->withCount('variants')->orderBy('id', 'desc')->paginate(15);
    return response()->json($products);
}

As a result, You'll get product with variants and count as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM