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){
113113 /**
114114 * Decodes slug to id
115115 * @param string $slug
116- * @return int
116+ * @return int|null
117117 */
118118 private static function decodeSlug ($ slug ){
119119 $ hashids = static ::getHashids ();
120120
121- $ id = ( int ) $ hashids ->decode ($ slug )[ 0 ] ;
121+ $ decoded = $ hashids ->decode ($ slug );
122122
123- return $ id ;
123+ if (! isset ($ decoded [0 ])){
124+ return null ;
125+ }
126+
127+ return (int ) $ decoded [0 ];
124128 }
125129
126130 /**
@@ -139,7 +143,7 @@ public static function findBySlugOrFail($slug){
139143 * Wrapper around Model::find
140144 *
141145 * @param string $slug
142- * @return \Illuminate\Database\Eloquent\Model
146+ * @return \Illuminate\Database\Eloquent\Model|null
143147 */
144148 public static function findBySlug ($ slug ){
145149 $ 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(){
8989 $ this ->assertEquals ($ post ->id , $ foundPost ->id );
9090 }
9191
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+
92103 /** @test */
93104 public function slugs_are_different_for_same_id_but_different_model (){
94105 $ post = Post::forceCreate (["title " => "title1 " ]);
You can’t perform that action at this time.
0 commit comments