This repository was archived by the owner on Jun 12, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -113,14 +113,18 @@ public function resolveRouteBinding($slug){
113
113
/**
114
114
* Decodes slug to id
115
115
* @param string $slug
116
- * @return int
116
+ * @return int|null
117
117
*/
118
118
private static function decodeSlug ($ slug ){
119
119
$ hashids = static ::getHashids ();
120
120
121
- $ id = ( int ) $ hashids ->decode ($ slug )[ 0 ] ;
121
+ $ decoded = $ hashids ->decode ($ slug );
122
122
123
- return $ id ;
123
+ if (! isset ($ decoded [0 ])){
124
+ return null ;
125
+ }
126
+
127
+ return (int ) $ decoded [0 ];
124
128
}
125
129
126
130
/**
@@ -139,7 +143,7 @@ public static function findBySlugOrFail($slug){
139
143
* Wrapper around Model::find
140
144
*
141
145
* @param string $slug
142
- * @return \Illuminate\Database\Eloquent\Model
146
+ * @return \Illuminate\Database\Eloquent\Model|null
143
147
*/
144
148
public static function findBySlug ($ slug ){
145
149
$ id = static ::decodeSlug ($ slug );
Original file line number Diff line number Diff line change @@ -89,6 +89,17 @@ public function model_can_be_found_by_slug(){
89
89
$ this ->assertEquals ($ post ->id , $ foundPost ->id );
90
90
}
91
91
92
+ /** @test */
93
+ public function find_byinvalid_slug_returns_null (){
94
+ $ post = Post::forceCreate (["title " => "title1 " ]);
95
+
96
+ $ slug = 'XX ' . $ post ->slug ();
97
+
98
+ $ foundPost = Post::findBySlug ($ slug );
99
+
100
+ $ this ->assertNull ($ foundPost );
101
+ }
102
+
92
103
/** @test */
93
104
public function slugs_are_different_for_same_id_but_different_model (){
94
105
$ post = Post::forceCreate (["title " => "title1 " ]);
You can’t perform that action at this time.
0 commit comments