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
efcb1ff2
Commit
efcb1ff2
authored
Nov 09, 2022
by
TTS Tran Viet Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
using scope
parent
a8178db3
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
13 deletions
+34
-13
HomeController.php
app/Http/Controllers/HomeController.php
+12
-11
Post.php
app/Models/Post.php
+6
-1
PostCategory.php
app/Models/PostCategory.php
+5
-0
Product.php
app/Models/Product.php
+6
-1
ProductCategory.php
app/Models/ProductCategory.php
+5
-0
No files found.
app/Http/Controllers/HomeController.php
View file @
efcb1ff2
...
...
@@ -16,16 +16,18 @@ class HomeController extends Controller
*/
public
function
index
()
{
$productCategories
=
ProductCategory
::
query
()
->
where
(
'parent_id'
,
'='
,
null
)
->
get
();
$latestDrops
=
Product
::
query
()
->
with
(
'productAttributes'
)
$productCategories
=
ProductCategory
::
noParent
()
->
get
();
$latestDrops
=
Product
::
withAttributes
()
->
statusEnabled
()
->
latest
()
->
take
(
4
)
->
get
();
$products
=
Product
::
query
()
->
latest
()
->
take
(
8
)
->
get
();
$latestBlogs
=
Post
::
query
()
->
with
(
'user'
)
$products
=
Product
::
withAttributes
()
->
statusEnabled
()
->
latest
()
->
take
(
8
)
->
get
();
$latestBlogs
=
Post
::
withUser
()
->
statusPublished
()
->
latest
()
->
take
(
3
)
...
...
@@ -35,13 +37,12 @@ public function index()
public
function
getProductsByCategory
(
Request
$request
)
{
$cats
=
ProductCategory
::
query
()
->
where
(
'id'
,
'='
,
$request
->
cat
)
->
orWhere
(
'parent_id'
,
'='
,
$request
->
cat
)
$cat
=
$request
->
cat
;
$subCats
=
ProductCategory
::
where
(
'parent_id'
,
$cat
)
->
get
(
'id'
)
->
toArray
();
$productsByCategory
=
Product
::
query
()
->
whereIn
(
'product_category_id'
,
$c
ats
)
array_push
(
$subCats
,
$cat
);
$productsByCategory
=
Product
::
whereIn
(
'product_category_id'
,
$subC
ats
)
->
latest
()
->
take
(
8
)
->
get
();
...
...
app/Models/Post.php
View file @
efcb1ff2
...
...
@@ -29,6 +29,11 @@ public function user()
public
function
scopeStatusPublished
(
$query
)
{
return
$query
->
where
(
'status'
,
'='
,
Post
::
STATUS_PUBLISHED
);
return
$query
->
where
(
'status'
,
Post
::
STATUS_PUBLISHED
);
}
public
function
scopeWithUser
(
$query
)
{
return
$query
->
with
(
'user'
);
}
}
app/Models/PostCategory.php
View file @
efcb1ff2
...
...
@@ -30,4 +30,9 @@ public function children()
{
return
$this
->
belongsTo
(
PostCategory
::
class
,
'parent_id'
);
}
public
function
scopeNoParent
(
$query
)
{
return
$query
->
where
(
'parent_id'
,
null
);
}
}
app/Models/Product.php
View file @
efcb1ff2
...
...
@@ -28,6 +28,11 @@ public function productAttributes()
public
function
scopeStatusEnabled
(
$query
)
{
return
$query
->
where
(
'status'
,
'='
,
Product
::
STATUS_ENABLED
);
return
$query
->
where
(
'status'
,
Product
::
STATUS_ENABLED
);
}
public
function
scopeWithAttributes
(
$query
)
{
return
$query
->
with
(
'productAttributes'
);
}
}
app/Models/ProductCategory.php
View file @
efcb1ff2
...
...
@@ -30,4 +30,9 @@ public function children()
{
return
$this
->
belongsTo
(
ProductCategory
::
class
,
'parent_id'
);
}
public
function
scopeNoParent
(
$query
)
{
return
$query
->
where
(
'parent_id'
,
null
);
}
}
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