blog api created
This commit is contained in:
1 parent
4f101885a8
commit
d98bc7bbb0
27 files changed
+1022
-44
No files matched your search
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Category;
|
||||
use App\Models\Blog\Post;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* برگرداندن دادههای مورد نیاز برای صفحهی اصلی
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 1. 5 پست آخر
|
||||
$latestPosts = Post::with(['category', 'tags', 'author'])
|
||||
->orderBy('published_at', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
// 2. 5 دستهبندی آخر
|
||||
$latestCategories = Category::orderBy('created_at', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
// 3. 5 پست با بیشترین بازدید
|
||||
$mostViewedPosts = Post::with(['category', 'tags', 'author'])
|
||||
->orderBy('views', 'desc')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
// 4. 4 پست تصادفی
|
||||
$randomPosts = Post::with(['category', 'tags', 'author'])
|
||||
->inRandomOrder()
|
||||
->take(4)
|
||||
->get();
|
||||
|
||||
// بازگشت به صورت JSON با استفاده از منابع (Resources)
|
||||
return response()->json([
|
||||
'latest_posts' => PostResource::collection($latestPosts),
|
||||
'latest_categories' => CategoryResource::collection($latestCategories),
|
||||
'most_viewed_posts' => PostResource::collection($mostViewedPosts),
|
||||
'random_posts' => PostResource::collection($randomPosts),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user