dynamic url laravel

Solutions on MaxInterview for dynamic url laravel by the best coders in the world

showing results for - "dynamic url laravel"
Christian
13 Aug 2020
1public function showBySlug($slug) {
2    $post = Post::where('slug','=',$slug)->first();
3    // would use app/posts/show.blade.php
4    return View::make('posts.show')->with(array(  
5        'post' => $post,
6    ));
7}
8Then you can route it like this:
9
10Route::get('post/{slug}', 'PostsController@showBySlug')
11    ->where('slug', '[\-_A-Za-z]+');`
12...which has the added bonus of allowing you an easy way to link straight to the slug routes on an index page, for example:
13
14@foreach ($posts as $post)
15    <h2>{{ HTML::link(
16        action('PostsController@showBySlug', array($post->slug)),
17        $post->title
18    )}}</h2>
19@endforeach