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
ec549257
Commit
ec549257
authored
Nov 15, 2022
by
TTS Tran Viet Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get products and post by categories
parent
a9ebfe3e
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
146 additions
and
15 deletions
+146
-15
HomeController.php
app/Http/Controllers/HomeController.php
+38
-3
AppServiceProvider.php
app/Providers/AppServiceProvider.php
+2
-1
index.blade.php
resources/views/client/index.blade.php
+1
-1
categories.blade.php
resources/views/client/post/categories.blade.php
+13
-0
index.blade.php
resources/views/client/post/index.blade.php
+38
-0
categories.blade.php
resources/views/client/product/categories.blade.php
+13
-0
index.blade.php
resources/views/client/product/index.blade.php
+28
-0
navbar.blade.php
resources/views/layout/navbar.blade.php
+8
-8
web.php
routes/web.php
+5
-2
No files found.
app/Http/Controllers/HomeController.php
View file @
ec549257
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
App\Http\Controllers
;
namespace
App\Http\Controllers
;
use
App\Models\Post
;
use
App\Models\Post
;
use
App\Models\PostCategory
;
use
App\Models\Product
;
use
App\Models\Product
;
use
App\Models\ProductCategory
;
use
App\Models\ProductCategory
;
use
Illuminate\Http\Request
;
use
Illuminate\Http\Request
;
...
@@ -16,7 +17,8 @@ class HomeController extends Controller
...
@@ -16,7 +17,8 @@ class HomeController extends Controller
*/
*/
public
function
index
()
public
function
index
()
{
{
$productCategories
=
ProductCategory
::
noParent
()
->
get
();
$productCategories
=
ProductCategory
::
noParent
()
->
with
(
'children'
)
->
get
();
$postCategories
=
PostCategory
::
noParent
()
->
with
(
'children'
)
->
get
();
$latestDrops
=
Product
::
with
(
'productAttributes'
)
$latestDrops
=
Product
::
with
(
'productAttributes'
)
->
enabled
()
->
enabled
()
->
latest
()
->
latest
()
...
@@ -32,16 +34,49 @@ public function index()
...
@@ -32,16 +34,49 @@ public function index()
->
latest
()
->
latest
()
->
take
(
3
)
->
take
(
3
)
->
get
();
->
get
();
return
view
(
'client.index'
,
compact
(
'latestDrops'
,
'productCategories'
,
'products'
,
'latestBlogs'
));
return
view
(
'client.index'
,
compact
(
'latestDrops'
,
'productCategories'
,
'p
ostCategories'
,
'p
roducts'
,
'latestBlogs'
));
}
}
public
function
getProducts
ByCategory
(
Request
$request
)
public
function
getProducts
Ajax
(
Request
$request
)
{
{
$category
=
ProductCategory
::
findOrFail
(
$request
->
cat
);
$category
=
ProductCategory
::
findOrFail
(
$request
->
cat
);
$productsByCategory
=
Product
::
ofCategories
(
$category
->
ids
)
$productsByCategory
=
Product
::
ofCategories
(
$category
->
ids
)
->
enabled
()
->
latest
()
->
latest
()
->
take
(
8
)
->
take
(
8
)
->
get
();
->
get
();
return
view
(
'client.products_by_category'
,
compact
(
'productsByCategory'
));
return
view
(
'client.products_by_category'
,
compact
(
'productsByCategory'
));
}
}
public
function
getProductsByCategory
(
Request
$request
,
$slug
)
{
$limit
=
$request
->
input
(
'limit'
,
10
);
$productCategories
=
ProductCategory
::
noParent
()
->
with
(
'children'
)
->
get
();
$postCategories
=
PostCategory
::
noParent
()
->
with
(
'children'
)
->
get
();
$category
=
ProductCategory
::
where
(
'slug'
,
$slug
)
->
firstOrFail
();
$productsByCategory
=
Product
::
with
(
'productAttributes'
)
->
enabled
()
->
ofCategories
(
$category
->
ids
)
->
paginate
(
$limit
);
return
view
(
'client.product.index'
,
compact
(
'productCategories'
,
'postCategories'
,
'productsByCategory'
));
}
public
function
getPostsByCategory
(
Request
$request
,
$slug
)
{
$limit
=
$request
->
input
(
'limit'
,
10
);
$productCategories
=
ProductCategory
::
noParent
()
->
with
(
'children'
)
->
get
();
$postCategories
=
PostCategory
::
noParent
()
->
with
(
'children'
)
->
get
();
$category
=
PostCategory
::
where
(
'slug'
,
$slug
)
->
firstOrFail
();
$recentPosts
=
Post
::
with
(
'user'
)
->
published
()
->
orderBy
(
'publish_date'
,
'desc'
)
->
take
(
5
)
->
get
();
$postsByCategory
=
Post
::
with
(
'user'
)
->
published
()
->
ofCategories
(
$category
->
ids
)
->
orderBy
(
'publish_date'
,
'desc'
)
->
paginate
(
$limit
);
return
view
(
'client.post.index'
,
compact
(
'productCategories'
,
'postCategories'
,
'recentPosts'
,
'postsByCategory'
));
}
}
}
app/Providers/AppServiceProvider.php
View file @
ec549257
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
App\Providers
;
namespace
App\Providers
;
use
Illuminate\Pagination\Paginator
;
use
Illuminate\Support\ServiceProvider
;
use
Illuminate\Support\ServiceProvider
;
class
AppServiceProvider
extends
ServiceProvider
class
AppServiceProvider
extends
ServiceProvider
...
@@ -23,6 +24,6 @@ public function register()
...
@@ -23,6 +24,6 @@ public function register()
*/
*/
public
function
boot
()
public
function
boot
()
{
{
//
Paginator
::
useBootstrap
();
}
}
}
}
resources/views/client/index.blade.php
View file @
ec549257
...
@@ -105,7 +105,7 @@
...
@@ -105,7 +105,7 @@
$
(
'#productCategory{{ $productCategory->id }}'
)
.
click
(
function
()
{
$
(
'#productCategory{{ $productCategory->id }}'
)
.
click
(
function
()
{
$
.
ajax
({
$
.
ajax
({
type
:
"GET"
,
type
:
"GET"
,
url
:
"{{ route('products
ByCategory
') }}"
,
url
:
"{{ route('products
Ajax
') }}"
,
data
:
{
data
:
{
cat
:
{{
$productCategory
->
id
}},
cat
:
{{
$productCategory
->
id
}},
},
},
...
...
resources/views/client/post/categories.blade.php
0 → 100644
View file @
ec549257
<li
style=
"list-style: none;"
value=
"{{ $postCategory->id }}"
>
<a
class=
"dropdown-item"
href=
"{{ route('postByCategory', ['slug' => $postCategory->slug]) }}"
>
{{ $postCategory->name }}
</a>
@if (count($postCategory->children) > 0)
<ul>
@foreach ($postCategory->children as $sub)
@include('client.post.categories', ['postCategory' => $sub])
@endforeach
</ul>
@endif
</li>
resources/views/client/post/index.blade.php
0 → 100644
View file @
ec549257
@
extends
(
'layout.app'
)
@
section
(
'content'
)
<
div
class
=
"container my-5"
>
<
div
class
=
"row"
>
<
div
class
=
"col-4"
>
<
h2
>
Recent
Post
</
h2
>
@
foreach
(
$recentPosts
as
$recentPost
)
<
div
class
=
"row my-3"
>
<
div
class
=
"col-6"
><
img
src
=
"{{
$recentPost->image
}}"
class
=
"card-img-top"
alt
=
"..."
></
div
>
<
div
class
=
"col-6"
>
<
span
>
{{
$recentPost
->
publish_date
}}
</
span
>
{{
--
<
h6
class
=
"card-title"
>
{{
$recentPost
->
title
}}
</
h6
>
--
}}
<
p
>
Posted
by
:
{{
$recentPost
->
user
->
name
}}
</
p
>
</
div
>
</
div
>
@
endforeach
</
div
>
<
div
class
=
"col-8"
>
<
h2
>
Blog
</
h2
>
@
foreach
(
$postsByCategory
as
$postByCategory
)
<
div
class
=
"row my-3"
>
<
div
class
=
"col-6"
><
img
src
=
"{{
$postByCategory->image
}}"
class
=
"card-img-top"
alt
=
"..."
>
</
div
>
<
div
class
=
"col-6"
>
<
span
>
{{
$postByCategory
->
publish_date
}}
</
span
>
<
h4
class
=
"card-title"
>
{{
$postByCategory
->
title
}}
</
h4
>
<
p
class
=
"mt-3"
>
Posted
by
:
{{
$postByCategory
->
user
->
name
}}
</
p
>
<
span
>
{{
$postByCategory
->
content
}}
</
span
>
</
div
>
</
div
>
@
endforeach
<
div
class
=
"paginator m-3"
>
{{
$postsByCategory
->
links
()
}}
</
div
>
</
div
>
</
div
>
</
div
>
@
endsection
resources/views/client/product/categories.blade.php
0 → 100644
View file @
ec549257
<li
style=
"list-style: none;"
value=
"{{ $productCategory->id }}"
>
<a
class=
"dropdown-item"
href=
"{{ route('productByCategory', ['slug' => $productCategory->slug]) }}"
>
{{ $productCategory->name }}
</a>
@if (count($productCategory->children) > 0)
<ul>
@foreach ($productCategory->children as $subCategory)
@include('client.product.categories', ['productCategory' => $subCategory])
@endforeach
</ul>
@endif
</li>
resources/views/client/product/index.blade.php
0 → 100644
View file @
ec549257
@
extends
(
'layout.app'
)
@
section
(
'content'
)
<
div
class
=
"container"
>
<
div
class
=
"row mx-1 my-5"
>
@
foreach
(
$productsByCategory
as
$productByCategory
)
<
div
class
=
"col-3 my-2"
>
<
div
class
=
"card"
>
<
img
src
=
"{{
$productByCategory->image
}}"
class
=
"card-img-top"
alt
=
"..."
>
<
div
class
=
"card-body"
>
<
h6
class
=
"card-title"
>
{{
$productByCategory
->
name
}}
</
h6
>
<
p
style
=
"max-height: 50px; overflow: auto;"
>
{{
$productByCategory
->
description
}}
</
p
>
<
h5
>
{{
$productByCategory
->
price
}}
</
h5
>
<
div
class
=
"color"
>
@
foreach
(
$productByCategory
->
productAttributes
as
$productAttribute
)
<
span
class
=
"dot mx-"
style
=
"height: 20px; width: 20px;background-color:{{
$productAttribute->color
}}; border-radius: 50%;
display: inline-block;"
></
span
>
@
endforeach
</
div
>
</
div
>
</
div
>
</
div
>
@
endforeach
</
div
>
<
div
class
=
"paginator m-3"
>
{{
$productsByCategory
->
links
()
}}
</
div
>
</
div
>
@
endsection
resources/views/layout/navbar.blade.php
View file @
ec549257
<nav
class=
"navbar navbar-expand-lg bg-light"
>
<nav
class=
"navbar navbar-expand-lg bg-light"
>
<div
class=
"container-fluid"
>
<div
class=
"container-fluid"
>
<a
class=
"navbar-brand"
href=
"
#
"
>
<a
class=
"navbar-brand"
href=
"
{{ route('home') }}
"
>
<img
src=
"{{ asset('assets') }}/images/logo.png"
alt=
""
srcset=
""
>
<img
src=
"{{ asset('assets') }}/images/logo.png"
alt=
""
srcset=
""
>
</a>
</a>
<button
class=
"navbar-toggler"
type=
"button"
data-bs-toggle=
"collapse"
data-bs-target=
"#navbarSupportedContent"
<button
class=
"navbar-toggler"
type=
"button"
data-bs-toggle=
"collapse"
data-bs-target=
"#navbarSupportedContent"
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
<div
class=
"collapse navbar-collapse"
id=
"navbarSupportedContent"
>
<div
class=
"collapse navbar-collapse"
id=
"navbarSupportedContent"
>
<ul
class=
"navbar-nav me-auto mb-2 mb-lg-0"
>
<ul
class=
"navbar-nav me-auto mb-2 mb-lg-0"
>
<li
class=
"nav-item"
>
<li
class=
"nav-item"
>
<a
class=
"nav-link active"
aria-current=
"page"
href=
"
#
"
>
Home
</a>
<a
class=
"nav-link active"
aria-current=
"page"
href=
"
{{ route('home') }}
"
>
Home
</a>
</li>
</li>
<li
class=
"nav-item dropdown"
>
<li
class=
"nav-item dropdown"
>
<a
class=
"nav-link dropdown-toggle"
href=
"#"
role=
"button"
data-bs-toggle=
"dropdown"
<a
class=
"nav-link dropdown-toggle"
href=
"#"
role=
"button"
data-bs-toggle=
"dropdown"
...
@@ -29,9 +29,9 @@
...
@@ -29,9 +29,9 @@
Product
Product
</a>
</a>
<ul
class=
"dropdown-menu"
>
<ul
class=
"dropdown-menu"
>
<li><a
class=
"dropdown-item"
href=
"#"
>
Action
</a></li>
@foreach ($productCategories as $productCategory)
<li><a
class=
"dropdown-item"
href=
"#"
>
Another action
</a></li>
@include('client.product.categories', ['productCategory' => $productCategory])
<li><a
class=
"dropdown-item"
href=
"#"
>
Something else here
</a></li>
@endforeach
</ul>
</ul>
</li>
</li>
<li
class=
"nav-item dropdown"
>
<li
class=
"nav-item dropdown"
>
...
@@ -40,9 +40,9 @@
...
@@ -40,9 +40,9 @@
Blog
Blog
</a>
</a>
<ul
class=
"dropdown-menu"
>
<ul
class=
"dropdown-menu"
>
<li><a
class=
"dropdown-item"
href=
"#"
>
Action
</a></li>
@foreach ($postCategories as $postCategory)
<li><a
class=
"dropdown-item"
href=
"#"
>
Another action
</a></li>
@include('client.post.categories', ['postCategory' => $postCategory])
<li><a
class=
"dropdown-item"
href=
"#"
>
Something else here
</a></li>
@endforeach
</ul>
</ul>
</li>
</li>
<li
class=
"nav-item"
>
<li
class=
"nav-item"
>
...
...
routes/web.php
View file @
ec549257
<?php
<?php
use
App\Http\Controllers\HomeController
;
use
App\Http\Controllers\HomeController
;
use
App\Http\Controllers\ProductController
;
use
Illuminate\Support\Facades\Route
;
use
Illuminate\Support\Facades\Route
;
/*
/*
...
@@ -15,6 +16,8 @@
...
@@ -15,6 +16,8 @@
*/
*/
Route
::
controller
(
HomeController
::
class
)
->
group
(
function
()
{
Route
::
controller
(
HomeController
::
class
)
->
group
(
function
()
{
Route
::
get
(
'/'
,
'index'
);
Route
::
get
(
'/'
,
'index'
)
->
name
(
'home'
);
Route
::
get
(
'/productsByCategory'
,
'getProductsByCategory'
)
->
name
(
'productsByCategory'
);
Route
::
get
(
'/productsAjax'
,
'getProductsAjax'
)
->
name
(
'productsAjax'
);
Route
::
get
(
'/san_pham/{slug}'
,
'getProductsByCategory'
)
->
name
(
'productByCategory'
);
Route
::
get
(
'/bai_viet/{slug}'
,
'getPostsByCategory'
)
->
name
(
'postByCategory'
);
});
});
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