Commit e5a4b315 authored by TTS Tran Viet Anh's avatar TTS Tran Viet Anh

posts sort

parent 5d815c8e
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$limit = $request->input("limit", 10);
$posts = Post::with('user')->paginate($limit);
return view('admin.posts.index', compact('posts'));
}
public function getPostsAjax(Request $request)
{
$limit = $request->input("limit", 10);
$option = $request->option;
switch ($option) {
case 1:
$posts = Post::with('user')->orderBy('id', 'desc')->paginate($limit);
break;
case 2:
$posts = Post::with('user')->withImage()->paginate($limit);
break;
case 3:
$posts = Post::with('user')->withoutImage()->paginate($limit);
break;
default:
$posts = Post::with('user')->paginate($limit);
break;
}
return view('admin.posts.posts_sorted', compact('posts'));
}
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Cviebrock\EloquentSluggable\Sluggable; use Cviebrock\EloquentSluggable\Sluggable;
class Post extends Model class Post extends Model
{ {
const STATUS_DRAFT = 1; const STATUS_DRAFT = 1;
const STATUS_PUBLISHED = 2; const STATUS_PUBLISHED = 2;
...@@ -38,6 +38,16 @@ public function scopeOfCategories($query, $cat_ids) ...@@ -38,6 +38,16 @@ public function scopeOfCategories($query, $cat_ids)
return $query->whereIn('post_category_id', $cat_ids); return $query->whereIn('post_category_id', $cat_ids);
} }
public function scopeWithImage($query)
{
return $query->whereNotNull('image');
}
public function scopeWithoutImage($query)
{
return $query->whereNull('image');
}
public function sluggable() public function sluggable()
{ {
return [ return [
......
img {
width: 200px;
height: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
{{-- boostrap --}}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous">
</script>
{{-- CSS --}}
<link rel="stylesheet" href="{{ asset('assets') }}/css/admin.css">
{{-- Jquery --}}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
{{-- Navbar --}}
@include('admin.layouts.navbar')
{{-- Navbar --}}
{{-- Content --}}
<div class="container">
<div class="content">
@yield('content')
</div>
</div>
{{-- Content --}}
</body>
</html>
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Admin</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="#">Products</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Post</a>
</li>
</ul>
<ul class="navbar-nav mb-2 mb-lg-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-expanded="false">
Account
</a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
@extends('admin.layouts.app')
@section('content')
<div class="container">
<div class="nav col-3 my-3">
<select class="form-select form-select-sm" aria-label=".form-select-sm example">
<option value="0">All Post</option>
<option value="1">Post by id</option>
<option value="2">Post with image</option>
<option value="3">Post without image</option>
</select>
</div>
<div class="posts-list">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Image</th>
<th scope="col">Author</th>
<th scope="col">Publish date</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
@foreach ($posts as $post)
<tr>
<th scope="row">{{ $post->id }}</th>
<td>{{ $post->title }}</td>
<td>
@if ($post->image == null)
<img src="{{ asset('assets') }}/images/no_image.jpg" alt="" class="rounded">
@else
<img src="{{ $post->image }}" alt="" class="rounded">
@endif
</td>
<td>{{ $post->user->name }}</td>
<td>{{ $post->publish_date }}</td>
<td>{{ $post->status }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $posts->links() }}
</div>
</div>
<script>
function search(page = 1) {
$.ajax({
type: "GET",
url: "{{ route('admin.postsAjax') }}",
data: {
option: $('select').val(),
page: page,
},
success: function(data) {
$(".posts-list").html(data);
}
});
}
$(document).ready(function() {
$('body').delegate('.pagination .page-link', 'click', function(e){
e.preventDefault()
search($(this).text())
})
$('select').change(function() {
search()
});
});
</script>
@endsection
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Image</th>
<th scope="col">Author</th>
<th scope="col">Publish date</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
@foreach ($posts as $post)
<tr>
<th scope="row">{{ $post->id }}</th>
<td>{{ $post->title }}</td>
<td>
@if ($post->image == null)
<img src="{{ asset('assets') }}/images/no_image.jpg" alt="" class="rounded">
@else
<img src="{{ $post->image }}" alt="" class="rounded">
@endif
</td>
<td>{{ $post->user->name }}</td>
<td>{{ $post->publish_date }}</td>
<td>{{ $post->status }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $posts->links() }}
<?php <?php
use App\Http\Controllers\HomeController; use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\ProductController; use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
...@@ -21,3 +22,11 @@ ...@@ -21,3 +22,11 @@
Route::get('/san-pham/{slug}', 'getProductsByCategory')->name('productByCategory'); Route::get('/san-pham/{slug}', 'getProductsByCategory')->name('productByCategory');
Route::get('/bai-viet/{slug}', 'getPostsByCategory')->name('postByCategory'); Route::get('/bai-viet/{slug}', 'getPostsByCategory')->name('postByCategory');
}); });
// Admin
Route::prefix('quan-ly')->name('admin.')->group(function () {
Route::controller(PostController::class)->group(function () {
Route::get('/bai-viet', 'index')->name('index');
Route::get('/postsAjax','getPostsAjax')->name('postsAjax');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment