Add nb_articles par category

This commit is contained in:
Kongzibapt 2021-05-10 16:09:26 +02:00
parent fb54e9ca0c
commit c984a90427
2 changed files with 38 additions and 0 deletions

View file

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

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddNbArticlesToCategories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('categories', function (Blueprint $table) {
$table->integer('nb_articles');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('categories', function (Blueprint $table) {
//
});
}
}