@@ -30,6 +30,14 @@ However, although hashslug encoding depends on the app key, it cannot be exposed
30
30
composer require balping/laravel-hashslug
31
31
```
32
32
33
+ ### Versions
34
+
35
+ | Laravel | Hashslug |
36
+ | ---------| ----------|
37
+ | 5.4.\* | 1.0.\* |
38
+ | 5.5.\* | 1.1.\* |
39
+
40
+
33
41
## Usage
34
42
35
43
Include trait on a model that you wish to have hashid slugs to hide numeric incremental ids.
@@ -40,7 +48,7 @@ use Illuminate\Database\Eloquent\Model;
40
48
use Balping\HashSlug\HasHashSlug;
41
49
42
50
class Post extends Model {
43
- use HasHashSlug;
51
+ use HasHashSlug;
44
52
}
45
53
```
46
54
@@ -66,9 +74,9 @@ Then you can resolve the model by the slug.
66
74
// app/Http/Controllers/PostController.php
67
75
68
76
public function show($slug){
69
- $post = Post:findBySlugOrFail($slug);
77
+ $post = Post:findBySlugOrFail($slug);
70
78
71
- return view('post.show', compact('post'));
79
+ return view('post.show', compact('post'));
72
80
}
73
81
```
74
82
@@ -77,10 +85,31 @@ You can use [implicit model binding](https://laravel.com/docs/master/routing#imp
77
85
Just typehint models and they are automatically resolved:
78
86
79
87
``` php
88
+ // routes/web.php
89
+ Route::resource('/posts', 'PostController');
90
+
80
91
// app/Http/Controllers/PostController.php
92
+ public function show(Post $post){
93
+ return view('post.show', compact('post'));
94
+ }
95
+ ```
81
96
97
+ If you need [ explicit model binding] ( https://laravel.com/docs/master/routing#explicit-binding ) , that's also convenient:
98
+
99
+ ``` php
100
+ //app/Providers/RouteServiceProvider.php
101
+ public function boot(){
102
+ parent::boot();
103
+
104
+ Route::model('article', App\Post::class);
105
+ }
106
+
107
+ // routes/web.php
108
+ Route::resource('/articles', 'PostController');
109
+
110
+ // app/Http/Controllers/PostController.php
82
111
public function show(Post $post){
83
- return view('post.show', compact('post'));
112
+ return view('post.show', compact('post'));
84
113
}
85
114
```
86
115
0 commit comments