API dự án PI GROUP (ok)
<?php
use Botble\Blog\Models\Category;
use Botble\Blog\Models\Post;
use Botble\Page\Models\Page;
use Botble\Slug\Facades\SlugHelper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Botble\Slug\Models\Slug;
use Illuminate\Support\Facades\DB;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::get('/pages', function (Request $request) {
$posts = Page::where('status', 'published')
->orderBy('created_at', 'desc')
->select('id', 'name', 'created_at') // tuỳ chọn field
->paginate(10); // hoặc ->get() nếu không phân trang
return response()->json([
'data' => $posts,
]);
});
Route::get('/posts', function (Request $request) {
// Lấy ID chuyên mục 'human' từ bảng slugs
$excludedCategoryId = Slug::where('key', 'human')
->where('reference_type', Category::class)
->value('reference_id');
// Nếu không có chuyên mục "human", trả về bài viết bình thường
if (!$excludedCategoryId) {
$posts = Post::where('status', 'published')
->orderBy('created_at', 'desc')
->select('id', 'name', 'description', 'content', 'is_featured', 'image', 'created_at')
->paginate(10);
$posts->getCollection()->transform(function ($post) {
$slug = DB::table('slugs')
->where('reference_type', Post::class)
->where('reference_id', $post->id)
->value('key');
$post->slug = $slug;
return $post;
});
return response()->json(['data' => $posts]);
}
// Lấy danh sách post_id thuộc chuyên mục human
$excludedPostIds = DB::table('post_categories')
->where('category_id', $excludedCategoryId)
->pluck('post_id');
// Lấy các bài viết KHÔNG thuộc chuyên mục đó
$posts = Post::where('status', 'published')
->whereNotIn('id', $excludedPostIds)
->orderBy('created_at', 'desc')
->select('id', 'name', 'description', 'content', 'is_featured', 'image', 'created_at')
->paginate(10);
// Gắn slug cho bài viết
$posts->getCollection()->transform(function ($post) {
$slug = DB::table('slugs')
->where('reference_type', Post::class)
->where('reference_id', $post->id)
->value('key');
$post->slug = $slug;
return $post;
});
return response()->json(['data' => $posts]);
});
Route::get('/pages/{slug}', function ($slug) {
// Tìm slug từ bảng `slugs`
$slugRecord = Slug::where('key', $slug)
->where('reference_type', Page::class)
->first();
if (! $slugRecord) {
return response()->json(['message' => 'Page not found'], 404);
}
// Tìm page từ reference_id
$page = Page::where('id', $slugRecord->reference_id)
->where('status', 'published')
->first();
if (! $page) {
return response()->json(['message' => 'Page not found'], 404);
}
return response()->json([
'data' => [
'id' => $page->id,
'name' => $page->name,
'description' => $page->description,
'slug' => $slug,
'content' => $page->content,
'custom_fields' => get_custom_field($page),
]
]);
});
Route::get('/posts/{slug}/related', function ($slug) {
$slugRecord = Slug::where('key', $slug)
->where('reference_type', Post::class)
->first();
if (! $slugRecord) {
return response()->json(['message' => 'Post not found'], 404);
}
// Lấy bài viết hiện tại và chuyên mục của nó
$post = Post::with('categories')
->where('id', $slugRecord->reference_id)
->where('status', 'published')
->first();
if (! $post) {
return response()->json(['message' => 'Post not found'], 404);
}
// Lấy các ID chuyên mục của bài viết
$categoryIds = $post->categories->pluck('id');
// Tìm bài viết liên quan (cùng chuyên mục, không trùng chính nó)
$relatedPosts = Post::where('status', 'published')
->where('id', '!=', $post->id)
->whereHas('categories', function ($query) use ($categoryIds) {
$query->whereIn('categories.id', $categoryIds);
})
->orderBy('created_at', 'desc')
->take(6)
// ->select('id', 'name', 'slug', 'description', 'image', 'created_at')
->get();
$relatedPosts->transform(function ($post) {
$slug = DB::table('slugs')
->where('reference_type', Post::class)
->where('reference_id', $post->id)
->value('key');
$post->slug = $slug;
return $post;
});
return response()->json([
'data' => $relatedPosts,
]);
});
Route::get('/posts/{slug}', function ($slug) {
// Tìm slug từ bảng `slugs`
$slugRecord = Slug::where('key', $slug)
->where('reference_type', Post::class)
->first();
if (! $slugRecord) {
return response()->json(['message' => 'Page not found'], 404);
}
// Tìm page từ reference_id
$post = Post::where('id', $slugRecord->reference_id)
->where('status', 'published')
->first();
if (! $post) {
return response()->json(['message' => 'Page not found'], 404);
}
return response()->json([
'data' => [
'id' => $post->id,
'name' => $post->name,
'slug' => $slug,
'description' => $post->description,
'is_featured' => $post->is_featured,
'image' => $post->image,
'created_at' => $post->created_at,
'content' => $post->content,
'categories' => $post->categories->map(function ($category) {
return [
'id' => $category->id,
'name' => $category->name,
'slug' => $category->slug,
];
}),
]
]);
});
Route::get('/categories/{slug}/posts', function ($slug, Request $request) {
// Tìm chuyên mục theo slug
$category = DB::table('slugs')
->where('key', $slug)
->where('reference_type', Category::class)
->first();
$slugRecord = DB::table('slugs')
->where('key', $slug)
->where('reference_type', Category::class)
->first();
$categorys = Category::select('name', 'description')
->where('id', $slugRecord->reference_id)
->first();
// Lấy bài viết thuộc category
$categoryId = $category->reference_id;
$posts = Post::where('status', 'published')
->whereHas('categories', function ($query) use ($categoryId) {
$query->where('categories.id', $categoryId);
})
->orderBy('created_at', 'desc')
->select('id', 'name', 'description', 'content', 'is_featured', 'image', 'created_at')
->paginate(10);
// Map slug cho từng bài viết
$posts->getCollection()->transform(function ($post) {
$slug = DB::table('slugs')
->where('reference_type', Post::class)
->where('reference_id', $post->id)
->value('key');
$post->slug = $slug;
if (!$post->custom_fields) {
$post->custom_fields = get_custom_field($post);
}
return $post;
});
return response()->json([
'data' => $posts,
'category' => $categorys
]);
});
Route::post('/contactforconsultation', function (\Illuminate\Http\Request $request) {
\Botble\Contact\Models\Contact::create([
'name' => $request->input('yourName'),
'email' => $request->input('email'),
'phone' => $request->input('phone'),
'content' => $request->input('taxCode'),
'position' => $request->input('position'),
'cvFileName' => $request->input('cvFileName'),
'cvFileType' => $request->input('cvFileType'),
]);
return response()->json(['message' => 'Gửi thành công.']);
});
PreviousHeader Create CRUD, get Data from Post man (ok)Next[JSONWEBTOKEN] PHP JWT & REST API Authentication Tutorial: Login and Signup full (ok)
Last updated
Was this helpful?