FounderStartup's avatar

Slug not working

I am storing the blog as follows :

controller :

 Blog::insert([
                        'category_id' => $request->categoryid,
                        'post_title' => $request->post_title,
                        'slug' => Str::slug($request->post_title, '-'),
                        'post_image' => $save_url,
                        'post_details' => $request->post_details,
                        'created_at' => Carbon::now(),

blade

  <a href="{{ route('post.details',[$item->id, $item->slug]) }}" class="blog-post">

But strangely first few blogs were working with slug, but now new blogs are not working ?

0 likes
25 replies
Tray2's avatar

Does it store the slug in the database or is the slug field null?

FounderStartup's avatar

Yes it stores the slug. Initially 3 blogs slug worked perfectly. But I think 4th was having a duplicate 'title' which I deleted. I presume that after that slug stopped working .

Sinnbeck's avatar

Explain what it means that it does not work. Error? Wrong page? Can you show the route and an example of a url that does not work.

1 like
FounderStartup's avatar

@Sinnbeck

    Route::get('/post/details/{id}/{slug}', [BlogController::class, 'DetailsBlogPost'])->name('post.details');

Sinnbeck's avatar

@FounderStartup you aren't binding anything it seems so it shouldn't break like that. Can you show the controller method?

1 like
FounderStartup's avatar

@Sinnbeck

       public function DetailsBlogPost($id , $slug){

            $blogcategory = BlogCategory::latest()->get();
            $blogpost = Blog::findOrFail($id);
            $trendingblogs = Blog::where('views', '>', 0)->take(2)->get();
            $relatedblogs = Blog::with('category')->where('views', '>', 0)->take(2)->get();
            Blog::find($id)->increment('views');
            $blogcategoryspecific = BlogCategory::findOrFail($id);
            $blogcomments = BlogComment::with('user')->where('blog_id',$id)->where('status', '1')->get();
            $blogcommentscount = BlogComment::where('blog_id',$id)->where('status', '1')->count();
            return view('frontend.blog_details',compact('relatedblogs','blogcommentscount','trendingblogs','blogcomments','blogpost','blogcategory', 'blogcategoryspecific' ));
        }

Sinnbeck's avatar

@FounderStartup no I just mean, you don't use it for anything in the controller. So just make sure the id is correct

1 like
Sinnbeck's avatar

Try moving to route to the top in web.php. Maybe another route interfere with it

1 like
FounderStartup's avatar

@Sinnbeck Yes I tried that. It is showing correct id. Do I need to do add something in the model ? Or some specific type of slug field ?

Sinnbeck's avatar

@FounderStartup oh so that worked. Exelent. Now we can start debugging

Try this then

public function DetailsBlogPost($id , $slug){
 dd(Blog::find($id));
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I think I just spotted the error. You are using the same ID for looking up both blog and category?

Blog::findOrFail($id);
BlogCategory::findOrFail($id);
1 like
Sinnbeck's avatar

@FounderStartup Yeah. Often you will think that the error is related to one thing (slug) and it is actually related to something totally different (findOrFail using wrong id), so your brain only looks for issues with what you think is the problem. It is quite common :)

1 like
FounderStartup's avatar

@Sinnbeck True. I searched the net for bugs in 'slug' and kept looking for it. never thought for the 'id' issue :).

Please or to participate in this conversation.