pt carudi
pt carudi

Reputation: 25

how to make a slug that does not have a redirect to page 404 laravel

I tried a slug that was not in my data and got an error like this "ErrorException Trying to get property 'id' of non-object" in

videodetail.php

public function detail($slug)
{
    $video = Video::where('slug', $slug)->first();

    $blogKey = 'blog_' . $video->id;

    if(!Session::has($blogKey)) {
        $video->increment('view_count');
        Session::put($blogKey, 1);
    }

    $randomvideo = Video::whereHas('categories', function ($q) use ($video) {
        return $q->whereIn('name', $video->categories->pluck('name')); 
    })
    ->where('id', '!=', $video->id)
    ->approved()
    ->published()
    ->take(8)->get();
    return view('video.detail', compact('video', 'randomvideo'));
}

and i got this error "Call to a member function videos() on null" in category

public function videoByCategory($slug)
{
    $category = Category::where('slug', $slug)->first();
    $videos = $category->videos()->approved()->published()->paginate(21);
    return view('category.index', compact('category', 'videos'));
}

Upvotes: 0

Views: 149

Answers (1)

Poldo
Poldo

Reputation: 1932

Add checking to your category.

public function videoByCategory($slug)
{
    $category = Category::where('slug', $slug)->first();
    if ($category) {
      $videos = $category->videos()->approved()->published()->paginate(21);
      return view('category.index', compact('category', 'videos'));
    } else {
     // whatever you want to do
    }

}

Upvotes: 1

Related Questions