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
a9ebfe3e
Commit
a9ebfe3e
authored
Nov 15, 2022
by
TTS Tran Viet Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add slug
parent
7b963a78
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
59 additions
and
5 deletions
+59
-5
Post.php
app/Models/Post.php
+17
-2
PostCategory.php
app/Models/PostCategory.php
+16
-1
Product.php
app/Models/Product.php
+11
-1
ProductCategory.php
app/Models/ProductCategory.php
+11
-1
2022_10_31_070501_create_products_table.php
...se/migrations/2022_10_31_070501_create_products_table.php
+1
-0
2022_10_31_070520_create_posts_table.php
database/migrations/2022_10_31_070520_create_posts_table.php
+1
-0
2022_10_31_100142_create_product_categories_table.php
...ons/2022_10_31_100142_create_product_categories_table.php
+1
-0
2022_10_31_100202_create_post_categories_table.php
...ations/2022_10_31_100202_create_post_categories_table.php
+1
-0
No files found.
app/Models/Post.php
View file @
a9ebfe3e
...
@@ -5,15 +5,16 @@
...
@@ -5,15 +5,16 @@
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
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
;
const
STATUS_FUTURE_PUBLISH
=
3
;
const
STATUS_FUTURE_PUBLISH
=
3
;
const
STATUS_DELETED
=
4
;
const
STATUS_DELETED
=
4
;
use
HasFactory
, SoftDeletes
;
use
HasFactory
, SoftDeletes
, Sluggable
;
protected
$guarded
=
[];
protected
$guarded
=
[];
...
@@ -31,4 +32,18 @@ public function scopePublished($query)
...
@@ -31,4 +32,18 @@ public function scopePublished($query)
{
{
return
$query
->
where
(
'status'
,
Post
::
STATUS_PUBLISHED
);
return
$query
->
where
(
'status'
,
Post
::
STATUS_PUBLISHED
);
}
}
public
function
scopeOfCategories
(
$query
,
$cat_ids
)
{
return
$query
->
whereIn
(
'post_category_id'
,
$cat_ids
);
}
public
function
sluggable
()
{
return
[
'slug'
=>
[
'source'
=>
'title'
]
];
}
}
}
app/Models/PostCategory.php
View file @
a9ebfe3e
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Cviebrock\EloquentSluggable\Sluggable
;
class
PostCategory
extends
Model
class
PostCategory
extends
Model
{
{
...
@@ -12,7 +13,7 @@ class PostCategory extends Model
...
@@ -12,7 +13,7 @@ class PostCategory extends Model
const
STATUS_DISABLED
=
2
;
const
STATUS_DISABLED
=
2
;
const
STATUS_DELETED
=
3
;
const
STATUS_DELETED
=
3
;
use
HasFactory
, SoftDeletes
;
use
HasFactory
, SoftDeletes
, Sluggable
;
protected
$guarded
=
[];
protected
$guarded
=
[];
...
@@ -31,8 +32,22 @@ public function children()
...
@@ -31,8 +32,22 @@ public function children()
return
$this
->
hasMany
(
PostCategory
::
class
,
'parent_id'
);
return
$this
->
hasMany
(
PostCategory
::
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
->
whereNull
(
'parent_id'
);
return
$query
->
whereNull
(
'parent_id'
);
}
}
public
function
sluggable
()
:
array
{
return
[
'slug'
=>
[
'source'
=>
'name'
]
];
}
}
}
app/Models/Product.php
View file @
a9ebfe3e
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Cviebrock\EloquentSluggable\Sluggable
;
class
Product
extends
Model
class
Product
extends
Model
{
{
...
@@ -12,7 +13,7 @@ class Product extends Model
...
@@ -12,7 +13,7 @@ class Product extends Model
const
STATUS_DISABLED
=
2
;
const
STATUS_DISABLED
=
2
;
const
STATUS_DELETED
=
3
;
const
STATUS_DELETED
=
3
;
use
HasFactory
, SoftDeletes
;
use
HasFactory
, SoftDeletes
, Sluggable
;
protected
$guarded
=
[];
protected
$guarded
=
[];
...
@@ -35,4 +36,13 @@ public function scopeOfCategories($query, $cat_ids)
...
@@ -35,4 +36,13 @@ public function scopeOfCategories($query, $cat_ids)
{
{
return
$query
->
whereIn
(
'product_category_id'
,
$cat_ids
);
return
$query
->
whereIn
(
'product_category_id'
,
$cat_ids
);
}
}
public
function
sluggable
()
{
return
[
'slug'
=>
[
'source'
=>
'name'
]
];
}
}
}
app/Models/ProductCategory.php
View file @
a9ebfe3e
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Cviebrock\EloquentSluggable\Sluggable
;
class
ProductCategory
extends
Model
class
ProductCategory
extends
Model
{
{
...
@@ -12,7 +13,7 @@ class ProductCategory extends Model
...
@@ -12,7 +13,7 @@ class ProductCategory extends Model
const
STATUS_DISABLE
=
2
;
const
STATUS_DISABLE
=
2
;
const
STATUS_DELETED
=
3
;
const
STATUS_DELETED
=
3
;
use
HasFactory
, SoftDeletes
;
use
HasFactory
, SoftDeletes
, Sluggable
;
protected
$guarded
=
[];
protected
$guarded
=
[];
...
@@ -40,4 +41,13 @@ public function scopeNoParent($query)
...
@@ -40,4 +41,13 @@ public function scopeNoParent($query)
{
{
return
$query
->
whereNull
(
'parent_id'
);
return
$query
->
whereNull
(
'parent_id'
);
}
}
public
function
sluggable
()
:
array
{
return
[
'slug'
=>
[
'source'
=>
'name'
]
];
}
}
}
database/migrations/2022_10_31_070501_create_products_table.php
View file @
a9ebfe3e
...
@@ -23,6 +23,7 @@ public function up()
...
@@ -23,6 +23,7 @@ public function up()
$table
->
unsignedInteger
(
'product_category_id'
);
$table
->
unsignedInteger
(
'product_category_id'
);
$table
->
unsignedInteger
(
'stock'
);
$table
->
unsignedInteger
(
'stock'
);
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
Product
::
STATUS_ENABLED
);
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
Product
::
STATUS_ENABLED
);
$table
->
string
(
'slug'
)
->
nullable
();
$table
->
softDeletes
();
$table
->
softDeletes
();
$table
->
timestamps
();
$table
->
timestamps
();
});
});
...
...
database/migrations/2022_10_31_070520_create_posts_table.php
View file @
a9ebfe3e
...
@@ -23,6 +23,7 @@ public function up()
...
@@ -23,6 +23,7 @@ public function up()
$table
->
unsignedInteger
(
'post_category_id'
);
$table
->
unsignedInteger
(
'post_category_id'
);
$table
->
date
(
'publish_date'
)
->
nullable
();
$table
->
date
(
'publish_date'
)
->
nullable
();
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
Post
::
STATUS_DRAFT
);
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
Post
::
STATUS_DRAFT
);
$table
->
string
(
'slug'
)
->
nullable
();
$table
->
softDeletes
();
$table
->
softDeletes
();
$table
->
timestamps
();
$table
->
timestamps
();
});
});
...
...
database/migrations/2022_10_31_100142_create_product_categories_table.php
View file @
a9ebfe3e
...
@@ -21,6 +21,7 @@ public function up()
...
@@ -21,6 +21,7 @@ public function up()
$table
->
unsignedInteger
(
'parent_id'
)
->
nullable
();
$table
->
unsignedInteger
(
'parent_id'
)
->
nullable
();
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
ProductCategory
::
STATUS_ENABLED
);
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
ProductCategory
::
STATUS_ENABLED
);
$table
->
unsignedInteger
(
'ordering'
);
$table
->
unsignedInteger
(
'ordering'
);
$table
->
string
(
'slug'
)
->
nullable
();
$table
->
softDeletes
();
$table
->
softDeletes
();
$table
->
timestamps
();
$table
->
timestamps
();
});
});
...
...
database/migrations/2022_10_31_100202_create_post_categories_table.php
View file @
a9ebfe3e
...
@@ -21,6 +21,7 @@ public function up()
...
@@ -21,6 +21,7 @@ public function up()
$table
->
unsignedInteger
(
'parent_id'
)
->
nullable
();
$table
->
unsignedInteger
(
'parent_id'
)
->
nullable
();
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
PostCategory
::
STATUS_ENABLED
);
$table
->
unsignedTinyInteger
(
'status'
)
->
default
(
PostCategory
::
STATUS_ENABLED
);
$table
->
unsignedInteger
(
'ordering'
);
$table
->
unsignedInteger
(
'ordering'
);
$table
->
string
(
'slug'
)
->
nullable
();
$table
->
softDeletes
();
$table
->
softDeletes
();
$table
->
timestamps
();
$table
->
timestamps
();
});
});
...
...
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