简体   繁体   中英

Summary records with a specific value in Laravel

I am beginner in Laravel. I make my project in Laravel 8.

I have this code:

$items = $this->selectedProductIngredient
              ->where('single_product_analysis_id', $id)
              ->where('group_id', 0)
              ->where('type', 1)
              ->get();

This is my items / values for report.

In result I have something like this:

#items: array:3 [▼
    0 => App\Models\SelectedProductIngredient {#1964 ▼
      #fillable: array:6 [▶]
      #guarded: array:1 [▶]
      #dates: array:2 [▶]
      #casts: array:2 [▶]
      #connection: "mysql"
      #table: "selected_product_ingredients"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:9 [▼
        "id" => 304
        "product_id" => 10
        "ingredient_id" => 7
        "single_product_analysis_id" => 31
        "weight" => "11.000"
        "type" => "1"
        "group_id" => 0
        "created_at" => "2021-05-07 16:50:18"
        "updated_at" => "2021-05-07 16:50:18"
      ]
      #original: array:9 [▶]
      #changes: []
      #classCastCache: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
    }
    1 => App\Models\SelectedProductIngredient {#1963 ▶}
    2 => App\Models\SelectedProductIngredient {#1962 ▶}

My migration:

Schema::create('selected_product_ingredients', function (Blueprint $table) {
    $table->id();
    $table->bigInteger('product_id')->unsigned()->default(0);
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
    $table->bigInteger('ingredient_id')->unsigned()->default(0);
    $table->bigInteger('group_id')->unsigned()->default(0);
    $table->bigInteger('single_product_analysis_id')->unsigned()->default(0);
    $table->boolean('type')->default(false);
    $table->decimal('weight', 12, 3)->nullable()->default(0);
    $table->foreign('ingredient_id')->references('id')->on('laboratoryingredients')->onDelete('cascade');
    $table->timestamps();
});

And I create array for report:

$names = [];
$data = [];
foreach ($items as $item) {
    array_push($names, $item->ingredient_id);
    array_push($data, $item->weight);
}

I have 2 small problems:

  1. I have different values for ingredient_id in the database. (multiple records). I need to add them up weight based on ingredient_id .
  2. In foreach, I need get only first 15 summary weight (based on ingredient_id ) ordered by max weight DESC.

How can I make it?

Please help me.

So you need to get the sum of weight , group the results by ingredient_id and order the results based on the sum, and only get the first 15?

You should be able to achieve that through selectRaw('sum(weight)') , groupBy() , orderByDesc() , and limit()

$items = $this->selectedProductIngredient
              // ->select('other', 'columns', 'you', 'need')
              ->selectRaw('sum(weight) as weight_sum')
              ->where('single_product_analysis_id', $id)
              ->where('group_id', 0)
              ->where('type', 1)
              ->groupBy('ingredient_id')
              ->orderByDesc('weight_sum')
              ->limit(15)
              ->get();

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