Create TopicsController.php
This commit is contained in:
36
app/Http/Controllers/TopicsController.php
Normal file
36
app/Http/Controllers/TopicsController.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Topic;
|
||||
use App\Http\Resources\TopicResource;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class TopicsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Discover Topics
|
||||
*
|
||||
* Take Random 14 Topics from Database
|
||||
*
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return TopicResource::collection(Topic::inRandomOrder()->take(14)->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all the articles for a given topics
|
||||
*
|
||||
* @param mixed $topic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(Topic $topic) : JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'topic' => $topic,
|
||||
'articles' => $topic->articles()->paginate(8)
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user