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
ad218458
Commit
ad218458
authored
Nov 10, 2022
by
TTS Tran Viet Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix code and using accessor
parent
efcb1ff2
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
27 deletions
+23
-27
HomeController.php
app/Http/Controllers/HomeController.php
+8
-12
Post.php
app/Models/Post.php
+1
-6
PostCategory.php
app/Models/PostCategory.php
+3
-3
Product.php
app/Models/Product.php
+4
-4
ProductCategory.php
app/Models/ProductCategory.php
+7
-2
No files found.
app/Http/Controllers/HomeController.php
View file @
ad218458
...
@@ -17,18 +17,18 @@ class HomeController extends Controller
...
@@ -17,18 +17,18 @@ class HomeController extends Controller
public
function
index
()
public
function
index
()
{
{
$productCategories
=
ProductCategory
::
noParent
()
->
get
();
$productCategories
=
ProductCategory
::
noParent
()
->
get
();
$latestDrops
=
Product
::
with
Attributes
(
)
$latestDrops
=
Product
::
with
(
'productAttributes'
)
->
statusE
nabled
()
->
e
nabled
()
->
latest
()
->
latest
()
->
take
(
4
)
->
take
(
4
)
->
get
();
->
get
();
$products
=
Product
::
with
Attributes
(
)
$products
=
Product
::
with
(
'productAttributes'
)
->
statusE
nabled
()
->
e
nabled
()
->
latest
()
->
latest
()
->
take
(
8
)
->
take
(
8
)
->
get
();
->
get
();
$latestBlogs
=
Post
::
with
User
(
)
$latestBlogs
=
Post
::
with
(
'user'
)
->
statusP
ublished
()
->
p
ublished
()
->
latest
()
->
latest
()
->
take
(
3
)
->
take
(
3
)
->
get
();
->
get
();
...
@@ -37,12 +37,8 @@ public function index()
...
@@ -37,12 +37,8 @@ public function index()
public
function
getProductsByCategory
(
Request
$request
)
public
function
getProductsByCategory
(
Request
$request
)
{
{
$cat
=
$request
->
cat
;
$category
=
ProductCategory
::
findOrFail
(
$request
->
cat
);
$subCats
=
ProductCategory
::
where
(
'parent_id'
,
$cat
)
$productsByCategory
=
Product
::
ofCategories
(
$category
->
ids
)
->
get
(
'id'
)
->
toArray
();
array_push
(
$subCats
,
$cat
);
$productsByCategory
=
Product
::
whereIn
(
'product_category_id'
,
$subCats
)
->
latest
()
->
latest
()
->
take
(
8
)
->
take
(
8
)
->
get
();
->
get
();
...
...
app/Models/Post.php
View file @
ad218458
...
@@ -27,13 +27,8 @@ public function user()
...
@@ -27,13 +27,8 @@ public function user()
return
$this
->
belongsTo
(
User
::
class
);
return
$this
->
belongsTo
(
User
::
class
);
}
}
public
function
scope
Status
Published
(
$query
)
public
function
scopePublished
(
$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 @
ad218458
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
class
PostCategory
extends
Model
class
PostCategory
extends
Model
{
{
const
STATUS_ENABLED
=
1
;
const
STATUS_ENABLED
=
1
;
const
STATUS_DISABLE
=
2
;
const
STATUS_DISABLE
D
=
2
;
const
STATUS_DELETED
=
3
;
const
STATUS_DELETED
=
3
;
use
HasFactory
, SoftDeletes
;
use
HasFactory
, SoftDeletes
;
...
@@ -28,11 +28,11 @@ public function parent()
...
@@ -28,11 +28,11 @@ public function parent()
public
function
children
()
public
function
children
()
{
{
return
$this
->
belongsTo
(
PostCategory
::
class
,
'parent_id'
);
return
$this
->
hasMany
(
PostCategory
::
class
,
'parent_id'
);
}
}
public
function
scopeNoParent
(
$query
)
public
function
scopeNoParent
(
$query
)
{
{
return
$query
->
where
(
'parent_id'
,
null
);
return
$query
->
where
Null
(
'parent_id'
);
}
}
}
}
app/Models/Product.php
View file @
ad218458
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
class
Product
extends
Model
class
Product
extends
Model
{
{
const
STATUS_ENABLED
=
1
;
const
STATUS_ENABLED
=
1
;
const
STATUS_DISABLE
=
2
;
const
STATUS_DISABLE
D
=
2
;
const
STATUS_DELETED
=
3
;
const
STATUS_DELETED
=
3
;
use
HasFactory
, SoftDeletes
;
use
HasFactory
, SoftDeletes
;
...
@@ -26,13 +26,13 @@ public function productAttributes()
...
@@ -26,13 +26,13 @@ public function productAttributes()
return
$this
->
hasMany
(
ProductAttribute
::
class
);
return
$this
->
hasMany
(
ProductAttribute
::
class
);
}
}
public
function
scope
Status
Enabled
(
$query
)
public
function
scopeEnabled
(
$query
)
{
{
return
$query
->
where
(
'status'
,
Product
::
STATUS_ENABLED
);
return
$query
->
where
(
'status'
,
Product
::
STATUS_ENABLED
);
}
}
public
function
scope
WithAttributes
(
$query
)
public
function
scope
OfCategories
(
$query
,
$cat_ids
)
{
{
return
$query
->
w
ith
(
'productAttributes'
);
return
$query
->
w
hereIn
(
'product_category_id'
,
$cat_ids
);
}
}
}
}
app/Models/ProductCategory.php
View file @
ad218458
...
@@ -28,11 +28,16 @@ public function parent()
...
@@ -28,11 +28,16 @@ public function parent()
public
function
children
()
public
function
children
()
{
{
return
$this
->
belongsTo
(
ProductCategory
::
class
,
'parent_id'
);
return
$this
->
hasMany
(
ProductCategory
::
class
,
'parent_id'
);
}
public
function
getIdsAttribute
()
{
return
collect
(
$this
->
children
->
pluck
(
'id'
))
->
prepend
(
$this
->
id
)
->
all
();
}
}
public
function
scopeNoParent
(
$query
)
public
function
scopeNoParent
(
$query
)
{
{
return
$query
->
where
(
'parent_id'
,
null
);
return
$query
->
where
Null
(
'parent_id'
);
}
}
}
}
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