Search code examples
phplaraveleloquentitems

Get total of out of stock products, total price added to the inventory records for the user laravel


So I have this situation where I have an Items table

 Schema::create('items', function (Blueprint $table) {
        $table->id();
        $table->string("name", 255)->index();
        $table->float("price");
        $table->foreignId("business_id")->constrained()->cascadeOnDelete();
        $table->float('selling_price')->after('price');
        $table->bigInteger('quantity_in_stock')->after('price')->nullable();
        $table->boolean('unlimited')->after('quantity_in_stock')->default(false);
        $table->timestamp("last_purchase_date")->nullable();
        $table->timestamps();
    });

So I want to return the items of a specific user using the business he created I want to calculate the total price, total number of products that quantity in stock is zero. please help


Solution

  • DB::table('itmes')
             ->select('business_id',DB::raw('count(*) as total_number, sum('price') as total_price'))
             ->where('quantity_in_stock', 0)
             ->groupBy('business_id')
             ->get();