Compare commits
2 commits
5abe69ca5e
...
bb2b23f4a3
Author | SHA1 | Date | |
---|---|---|---|
|
bb2b23f4a3 | ||
|
c984a90427 |
2 changed files with 38 additions and 0 deletions
|
@ -18,6 +18,12 @@ class CategorieController extends Controller
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$categories = Categorie::all();
|
$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);
|
return response()->json($categories);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
//
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue