Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • balping/laravel-hashslug
  • aamirchaudhary/laravel-hashslug
  • jijoel/laravel-hashslug
  • rognales/laravel-hashslug
  • sdkakcy/laravel-hashslug
5 results
Select Git revision
Show changes
Commits on Source (1)
......@@ -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);
......
......@@ -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"]);
......