Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
L
laravel_training
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TTS Tran Viet Anh
laravel_training
Commits
e5a4b315
Commit
e5a4b315
authored
Nov 16, 2022
by
TTS Tran Viet Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
posts sort
parent
5d815c8e
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
239 additions
and
1 deletion
+239
-1
PostController.php
app/Http/Controllers/PostController.php
+42
-0
Post.php
app/Models/Post.php
+11
-1
admin.css
public/assets/css/admin.css
+4
-0
no_image.jpg
public/assets/images/no_image.jpg
+0
-0
app.blade.php
resources/views/admin/layouts/app.blade.php
+35
-0
navbar.blade.php
resources/views/admin/layouts/navbar.blade.php
+35
-0
index.blade.php
resources/views/admin/posts/index.blade.php
+72
-0
posts_sorted.blade.php
resources/views/admin/posts/posts_sorted.blade.php
+31
-0
web.php
routes/web.php
+9
-0
No files found.
app/Http/Controllers/PostController.php
0 → 100644
View file @
e5a4b315
<?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'
));
}
}
app/Models/Post.php
View file @
e5a4b315
...
...
@@ -7,7 +7,7 @@
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Cviebrock\EloquentSluggable\Sluggable
;
class
Post
extends
Model
class
Post
extends
Model
{
const
STATUS_DRAFT
=
1
;
const
STATUS_PUBLISHED
=
2
;
...
...
@@ -38,6 +38,16 @@ public function scopeOfCategories($query, $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
()
{
return
[
...
...
public/assets/css/admin.css
0 → 100644
View file @
e5a4b315
img
{
width
:
200px
;
height
:
auto
;
}
public/assets/images/no_image.jpg
0 → 100644
View file @
e5a4b315
16.4 KB
resources/views/admin/layouts/app.blade.php
0 → 100644
View file @
e5a4b315
<!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>
resources/views/admin/layouts/navbar.blade.php
0 → 100644
View file @
e5a4b315
<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>
resources/views/admin/posts/index.blade.php
0 → 100644
View file @
e5a4b315
@
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
resources/views/admin/posts/posts_sorted.blade.php
0 → 100644
View file @
e5a4b315
<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() }}
routes/web.php
View file @
e5a4b315
<?php
use
App\Http\Controllers\HomeController
;
use
App\Http\Controllers\PostController
;
use
App\Http\Controllers\ProductController
;
use
Illuminate\Support\Facades\Route
;
...
...
@@ -21,3 +22,11 @@
Route
::
get
(
'/san-pham/{slug}'
,
'getProductsByCategory'
)
->
name
(
'productByCategory'
);
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'
);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment