2

I have a blog list. When I click on a blog I want to make the URL as the blog name like : www.domain.com/blog/my-blog-name.

In my blog list I have the link as blog/blogid(2/3/4) ex : www.domain.com/blog/1

When someone visit www.domain.com/blog/1 then the URL should be www.domain.com/blog/my-blog-name { the blog name will change as per the blog Id }

I did so many research on this but no luck. It would be great if anyone help me to get out of this.

I am using laravel 5.4. I am new to laravel also.

6 Answers 6

4

You need to create a slug field in your database and you will find a post using slug instead of id

Define route:

Route::get('post/{slug}', 'PostsControler@findBySlug');

Example http://example.com/post/my-awesome-blog-post

and in your controller

//PostsController.php

public function findBySlug($slug)
{
    $post = Post::where('slug', $slug)->firstOrFail();

    // return view
}

I suggest you use Eloquent-Sluggable to auto create slug value from your title field

$post = new Post([
    'title' => 'My Awesome Blog Post',
]);
$post->save();
// $post->slug is "my-awesome-blog-post"
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I wanted. Thanks
2

You can create a unique slug or name for your blog , but since names can be duplicate and for the user it is difficult to manually create a unique slug, then I would advise you to do so

Route::get('/blog/{id}/{name}',  'BlogController@show');

this it will look nice in url, as here for example in stackoverflow!

Comments

1

You can just override the function getRouteKeyName

public function getRouteKeyName()
{
    return 'slug';
}

Comments

0

Try with following one!

In your routes file:

Route::get('blog/{slug}', 'BlogController@show'); // make sure not conflict with resource routes define

And then your BlogController:

public function show($slug)
{
    $id = explode($slug, '-')[0]; //get id of post

    // your logic
}

Hope this helps you!

5 Comments

Is slug here only id or id with the blog name?
Hi, slug with id with the blog name! For example: www.domain.com/blog/1-my-first-blog
Ok, but I don't want to show the id in my URL, I want to show only the Blog name, Is it possible ?
Yes, then you need to remove value before fist - Just try this first, then try to remove id!
Ok. Thank you I will try it
0

you can simply take your title from database and use it as a slug like below :

$slug = str_replace(' ','-',$yourtitle);

Comments

0

You can use Laravel mutators setSlugAttribute in your model:

public function setSlugAttribute($title){
  $this->attributes['slug'] =  str_replace(' ','-',$title);   
}

and write this sample code in your web.php :

Route::get('/blog/{slug}',  'BlogController@show');

You can use slug in your method.

public function findBySlug($slug){

$post = Post::whereSlug($slug)->firstOrFail();


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.