Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
L
laravel-hashslug
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
6
Snippets
Groups
Projects
Show more breadcrumbs
balping
laravel-hashslug
Compare revisions
1.1.1 to 1.1.2
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
balping/laravel-hashslug
Select target project
No results found
1.1.2
Select Git revision
Swap
Target
balping/laravel-hashslug
Select target project
balping/laravel-hashslug
aamirchaudhary/laravel-hashslug
jijoel/laravel-hashslug
rognales/laravel-hashslug
sdkakcy/laravel-hashslug
5 results
1.1.1
Select Git revision
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (1)
fix bug with invalid slug
· 22c8a2b9
balping
authored
7 years ago
22c8a2b9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/HasHashSlug.php
+8
-4
8 additions, 4 deletions
src/HasHashSlug.php
tests/HashSlugTest.php
+11
-0
11 additions, 0 deletions
tests/HashSlugTest.php
with
19 additions
and
4 deletions
src/HasHashSlug.php
View file @
22c8a2b9
...
...
@@ -113,14 +113,18 @@ trait HasHashSlug {
/**
* Decodes slug to id
* @param string $slug
* @return int
* @return int
|null
*/
private
static
function
decodeSlug
(
$slug
){
$hashids
=
static
::
getHashids
();
$
id
=
(
int
)
$hashids
->
decode
(
$slug
)
[
0
]
;
$
decoded
=
$hashids
->
decode
(
$slug
);
return
$id
;
if
(
!
isset
(
$decoded
[
0
])){
return
null
;
}
return
(
int
)
$decoded
[
0
];
}
/**
...
...
@@ -139,7 +143,7 @@ trait HasHashSlug {
* Wrapper around Model::find
*
* @param string $slug
* @return \Illuminate\Database\Eloquent\Model
* @return \Illuminate\Database\Eloquent\Model
|null
*/
public
static
function
findBySlug
(
$slug
){
$id
=
static
::
decodeSlug
(
$slug
);
...
...
This diff is collapsed.
Click to expand it.
tests/HashSlugTest.php
View file @
22c8a2b9
...
...
@@ -89,6 +89,17 @@ class HashSlugTest extends \Orchestra\Testbench\TestCase
$this
->
assertEquals
(
$post
->
id
,
$foundPost
->
id
);
}
/** @test */
public
function
find_byinvalid_slug_returns_null
(){
$post
=
Post
::
forceCreate
([
"title"
=>
"title1"
]);
$slug
=
'XX'
.
$post
->
slug
();
$foundPost
=
Post
::
findBySlug
(
$slug
);
$this
->
assertNull
(
$foundPost
);
}
/** @test */
public
function
slugs_are_different_for_same_id_but_different_model
(){
$post
=
Post
::
forceCreate
([
"title"
=>
"title1"
]);
...
...
This diff is collapsed.
Click to expand it.