Browse Source

Add nb_articles par category

Kongzibapt 2 months ago
parent
commit
c984a90427

+ 6
- 0
api_proximo/app/Http/Controllers/CategorieController.php View File

@@ -18,6 +18,12 @@ class CategorieController extends Controller
18 18
     public function index()
19 19
     {
20 20
         $categories = Categorie::all();
21
+
22
+        // Renvoie le nombre d'articles par categorie 
23
+        for($i = 0; $i < count($categories); ++$i) {
24
+            $categories[$i]['nb_articles'] += Article::where('category_id','=',$categories[$i]->id)->count();
25
+        }
26
+
21 27
         return response()->json($categories);
22 28
     }
23 29
 

+ 32
- 0
api_proximo/database/migrations/2021_05_10_125205_add_nb_articles_to_categories.php View File

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+class AddNbArticlesToCategories extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     *
12
+     * @return void
13
+     */
14
+    public function up()
15
+    {
16
+        Schema::table('categories', function (Blueprint $table) {
17
+            $table->integer('nb_articles');
18
+        });
19
+    }
20
+
21
+    /**
22
+     * Reverse the migrations.
23
+     *
24
+     * @return void
25
+     */
26
+    public function down()
27
+    {
28
+        Schema::table('categories', function (Blueprint $table) {
29
+            //
30
+        });
31
+    }
32
+}

Loading…
Cancel
Save