skip to Main Content

I have a route in web.php

Route::get('/{slug_category}', 'WelcomeController@cdetail')->name('cdetail');
Route::get('/{slug_category}/{slug}', 'WelcomeController@detail')->name('detail');

and my category slug in the database is action, movie

when I access /action or /movie its shows well. But when I access something like /test or /something slug that not in the database it shows an error like this

enter image description here

How to solve when I access /test showing error like "404 Page not found" rather than error like the image above ?

Here is my code

cdetail.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Movrev - Movie Review for all genre</title>
        <meta content="width=device-width, initial-scale=1.0" name="viewport">
        <meta content="Consulting Website Template Free Download" name="keywords">
        <meta content="Consulting Website Template Free Download" name="description">

        <!-- Favicon -->
        <link href="img/favicon.ico" rel="icon">

        <!-- Google Font -->
        <link href="https://fonts.googleapis.com/css2?family=Lato&family=Oswald:wght@200;300;400&display=swap" rel="stylesheet">
        
        <!-- CSS Libraries -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
        <link href="{{ asset('lib/animate/animate.min.css" rel="stylesheet')}}">
        <link href="{{ asset('lib/owlcarousel/assets/owl.carousel.min.css')}}" rel="stylesheet">

        <!-- Template Stylesheet -->
        <link href="{{ asset('css/style.css')}}" rel="stylesheet">
    </head>

    <body class="page">
        <!-- Nav Bar Start -->
        <div class="navbar navbar-expand-lg bg-dark navbar-dark">
            <div class="container-fluid">
                <a href="index.html" class="navbar-brand">Movie Review</a>
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
                    <div class="navbar-nav ml-auto">
                        <a href="{{route('welcome')}}" class="nav-item nav-link">Home</a>
                        <div class="nav-item dropdown">
                            <a href="#" class="nav-link dropdown-toggle active" data-toggle="dropdown">Category</a>
                            <div class="dropdown-menu">
                            <?php $i=0 ?>
                                @foreach($cat as $c)
                                <a href="{{route('cdetail',  $cat[$i]['slug'])}}" class="dropdown-item">{{$cat[$i]["name"]}}</a>
                                <?php $i++ ?>
                                @endforeach
                            </div>
                        </div>
                        @if (Route::has('login'))
                        @auth
                            <a href="{{ url('/home') }}" class="nav-item nav-link">Profile</a>
                                @else
                                <a href="{{ route('login') }}" class="nav-item nav-link">Login</a>

                                    @if (Route::has('register'))
                                        <a href="{{ route('register') }}" class="nav-item nav-link" >Register</a>
                                    @endif
                                @endauth
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
        <!-- Nav Bar End -->
        
        
        <!-- Blog Start -->
        <div class="blog blog-page mt-125">
            <div class="container">
                <div class="section-header">
                    <p>All reviews from category</p>
                    <h2>{{$category[0]["name"]}}</h2>
                </div>
                <div class="row">
                    <?php $i=0 ?>
                    @foreach($categories as $category)
                    <div class="col-md-6">
                        <div class="blog-item">
                            <div class="blog-img">
                                <img style="width: 100%;height: 20vw;object-fit: cover;" src="<?php echo asset("uploads/banner/".$categories[$i]["banner"])?>" alt="Blog">
                            </div>
                            <div class="blog-content">
                                <h2 class="blog-title">{{$categories[$i]["title"]}}</h2>
                                <div class="blog-meta">
                                    <i class="fa fa-list-alt"></i>
                                    <a href="">{{$categories[$i]["category"]["name"]}}</a>
                                    <i class="fa fa-calendar-alt"></i>
                                    <p>{{$categories[$i]["created_at"]->format('l, j F Y')}}</p>
                                </div>
                                <div class="blog-text">
                                    <p>
                                    {{str_limit($categories[$i]["content"], 100 ," ...")}}    
                                    </p>
                                    <a class="btn" href="{{ route('detail', [$categories[$i]['category']['slug'], $categories[$i]['slug']]) }}">Read More</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php $i++ ?>
                    @endforeach
                </div>  
            </div>
        </div>
        
        <!-- Blog End -->
    </body>
</html>

WelcomeController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppArticle;
use AppCategory;

class WelcomeController extends Controller
{
    public function welcome(){
        $articles = Article::orderBy('created_at', "desc")->paginate(5);
        $cat = Category::get();

        return view('welcome', compact('articles','cat'));
    }

    public function cdetail($slug_category){
        $categories = Article::with('category')->whereHas('category', function($q) use($slug_category) {
            $q->where('slug', '=', $slug_category);})
            ->get();
            $category = Category::where('slug', '=', $slug_category)->get();
            $cat = Category::get();
                
            return view ('cdetail', compact('categories','category','cat'));
    }

    public function detail($slug_category, $slug){
        $cat = Category::get();
        $article =  $categories = Article::with('category')->whereHas('category', function($q) use($slug_category) {
            $q->where('slug', '=', $slug_category);})
            ->where('slug', $slug)
            ->first();
    
        return view ('detail', compact('article','cat'));
    }
}


welcome.blade.php

<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Movrev - Movie Review for all genre</title>
        <meta content="width=device-width, initial-scale=1.0" name="viewport">
        <meta content="Consulting Website Template Free Download" name="keywords">
        <meta content="Consulting Website Template Free Download" name="description">

        <!-- Favicon -->
        <link href="img/favicon.ico" rel="icon">

        <!-- Google Font -->
        <link href="https://fonts.googleapis.com/css2?family=Lato&family=Oswald:wght@200;300;400&display=swap" rel="stylesheet">
        
        <!-- CSS Libraries -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
        <link href="{{ asset('lib/animate/animate.min.css" rel="stylesheet')}}">
        <link href="{{ asset('lib/owlcarousel/assets/owl.carousel.min.css')}}" rel="stylesheet">

        <!-- Template Stylesheet -->
        <link href="{{ asset('css/style.css')}}" rel="stylesheet">
    </head>

    <body>
        <!-- Nav Bar Start -->
        <div class="navbar navbar-expand-lg bg-dark navbar-dark">
            <div class="container-fluid">
                <a href="index.html" class="navbar-brand">Movie Review</a>
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
                    <div class="navbar-nav ml-auto">
                        <a href="{{route('welcome')}}" class="nav-item nav-link active">Home</a>
                        <div class="nav-item dropdown">
                            <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Category</a>
                            <div class="dropdown-menu">
                            <?php $i=0 ?>
                                @foreach($cat as $c)
                                <a href="{{route('cdetail',  $cat[$i]['slug'])}}" class="dropdown-item">{{$cat[$i]["name"]}}</a>
                                <?php $i++ ?>
                                @endforeach
                            </div>
                        </div>
                        @if (Route::has('login'))
                        @auth
                            <a href="{{ url('/home') }}" class="nav-item nav-link">Profile</a>
                                @else
                                <a href="{{ route('login') }}" class="nav-item nav-link">Login</a>

                                    @if (Route::has('register'))
                                        <a href="{{ route('register') }}" class="nav-item nav-link" >Register</a>
                                    @endif
                                @endauth
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
        <!-- Nav Bar End -->

        <!-- Carousel Start -->
        <div class="carousel">
            <div class="container-fluid">
                <div class="owl-carousel">
                    <div class="carousel-item">
                        <div class="carousel-img">
                            <img src="img/knives-out.png" alt="Image">
                        </div>
                        <div class="carousel-text">
                            <h1>Knives Out</h1>
                            <p>
                            Knives Out is a 2019 American mystery film written and directed by Rian Johnson, and produced by Johnson and Ram Bergman. It follows a master detective investigating the death of the patriarch of a wealthy, dysfunctional family.
                            </p>
                            <div class="carousel-btn">
                                <a class="btn btn-play" data-toggle="modal" data-src="https://www.youtube.com/embed/qOg3AoRc4nI" data-target="#videoModal"><i class="fa fa-play"></i>Watch Trailer</a>
                            </div>
                        </div>
                    </div>
                    <div class="carousel-item">
                        <div class="carousel-img">
                            <img src="img/the-gifted.jpg" alt="Image">
                        </div>
                        <div class="carousel-text">
                            <h1>The Gifted</h1>
                            <p>
                            Pawaret "Pang" Sermrittirong (Korapat Kirdpan) is a struggling, ordinary 10th grade student in Ritdha High School. Here, a classification system is strictly implemented wherein students are divided into classes based on academic excellence. Pang is in Class VIII, the lowest level, and has no hopes of rising up the hierarchy.
                            </p>
                            <div class="carousel-btn">
                                <a class="btn btn-play" data-toggle="modal" data-src="https://www.youtube.com/embed/1c3FJXZBmIk" data-target="#videoModal"><i class="fa fa-play"></i>Watch Trailer</a>
                            </div>
                        </div>
                    </div>
                    <div class="carousel-item">
                        <div class="carousel-img">
                            <img src="img/start-up.jpg" alt="Image">
                        </div>
                        <div class="carousel-text">
                            <h1>Start Up</h1>
                            <p>
                            Set in South Korea's fictional Silicon Valley called Sandbox, Start-Up tells the story of people in the world of startup companies.
                            Seo Dal-mi (Bae Suzy) is a bright and ambitious young woman who dreams of becoming Korea’s Steve Jobs. Dal-mi doesn’t have a fancy background but she’s passionate about her work. She has bright energy and is a person of great vitality, having experience in a wide range of part-time jobs.
                            </p>
                            <div class="carousel-btn">
                                <a class="btn btn-play" data-toggle="modal" data-src="https://www.youtube.com/embed/BemKyzbLDDc" data-target="#videoModal"><i class="fa fa-play"></i>Watch Trailer</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- Carousel End -->

        <!-- Video Modal Start-->
        <div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-body">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>        
                        <!-- 16:9 aspect ratio -->
                        <div class="embed-responsive embed-responsive-16by9">
                            <iframe class="embed-responsive-item" src="" id="video"  allowscriptaccess="always" allow="autoplay"></iframe>
                        </div>
                    </div>
                </div>
            </div>
        </div> 
        <!-- Video Modal End -->

        <!-- Blog Start -->
        <div class="blog">
            <div class="container">
                <div class="section-header">
                    <h2>Latest Movie Review</h2>
                </div>
                <div class="owl-carousel blog-carousel">
                @foreach($articles as $a)
                    <div class="blog-item">
                        <div class="blog-img">
                            <img style="width: 100%;height: 20vw;object-fit: cover;" src="<?php echo asset("uploads/banner/$a->banner")?>" alt="Blog">
                        </div>
                        <div class="blog-content">
                            <h2 class="blog-title">{{$a->title}}</h2>
                            <div class="blog-meta">
                                <i class="fa fa-list-alt"></i>
                                <a href="{{route('cdetail',  $a->category->slug)}}">{{$a->category->name}}</a>
                                <i class="fa fa-calendar-alt"></i>
                                <p><?php echo $a->created_at->format('l, j F Y')?></p>
                            </div>
                            <div class="blog-text">
                                <p>
                                {{str_limit($a->content, 100 ," ...")}}
                                </p>
                                <a class="btn" href="{{ route('detail', [$a->category->slug, $a->slug])}}">Read More</a>
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
        <!-- Blog End -->
    </body>
</html>

detail.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Movrev - Movie Review for all genre</title>
        <meta content="width=device-width, initial-scale=1.0" name="viewport">
        <meta content="Consulting Website Template Free Download" name="keywords">
        <meta content="Consulting Website Template Free Download" name="description">

        <!-- Favicon -->
        <link href="img/favicon.ico" rel="icon">

        <!-- Google Font -->
        <link href="https://fonts.googleapis.com/css2?family=Lato&family=Oswald:wght@200;300;400&display=swap" rel="stylesheet">
        
        <!-- CSS Libraries -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
        <link href="{{ asset('lib/animate/animate.min.css" rel="stylesheet')}}">
        <link href="{{ asset('lib/owlcarousel/assets/owl.carousel.min.css')}}" rel="stylesheet">

        <!-- Template Stylesheet -->
        <link href="{{ asset('css/style.css')}}" rel="stylesheet">
    </head>

    <body class="page">
        <!-- Nav Bar Start -->
        <div class="navbar navbar-expand-lg bg-dark navbar-dark">
            <div class="container-fluid">
                <a href="index.html" class="navbar-brand">Movie Review</a>
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
                    <div class="navbar-nav ml-auto">
                        <a href="{{route('welcome')}}" class="nav-item nav-link">Home</a>
                        <div class="nav-item dropdown">
                            <a href="#" class="nav-link dropdown-toggle active" data-toggle="dropdown">Category</a>
                            <div class="dropdown-menu">
                            <?php $i=0 ?>
                                @foreach($cat as $c)
                                <a href="{{route('cdetail',  $cat[$i]['slug'])}}" class="dropdown-item">{{$cat[$i]["name"]}}</a>
                                <?php $i++ ?>
                                @endforeach
                            </div>
                        </div>
                        @if (Route::has('login'))
                        @auth
                            <a href="{{ url('/home') }}" class="nav-item nav-link">Profile</a>
                                @else
                                <a href="{{ route('login') }}" class="nav-item nav-link">Login</a>

                                    @if (Route::has('register'))
                                        <a href="{{ route('register') }}" class="nav-item nav-link" >Register</a>
                                    @endif
                                @endauth
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
        <!-- Nav Bar End -->


        <!-- Single Page Start -->
        <div class="mt-125">
            <div class="container">
                <div class="section-header">
                    <p>Category: <a href="{{route('cdetail',  $article->category->slug)}}">{{$article->category->name}}</a></p>
                    <h2>{{$article->title}}</h2>
                </div>
                <div class="row">
                    <div class="col-12" style="text-align:center;">
                            <img style="height: 500px;
                            display: inline-block;margin:0 0 25px 0" src="<?php echo asset("uploads/banner/$article->banner")?>" alt="Image">
                    </div>
                    <p>
                        {{$article->content}}
                    </p>
                </div>
            </div>
        </div>
        <!-- Single Page End -->
    </body>
</html>

2

Answers


  1. It’s not because you have the parameter (movie/action) in your db but because you have blade files in your resourses/views.

    When you try for /test or /something, it looks for a blade file in your views as test.blade.php or something.blade.php and failing shows the error 404 not found.

    Login or Signup to reply.
  2. Try using firstOrFail which throws an exception if the model is not found.

    public function cdetail($slug_category){
        $category = Category::where('slug', $slug_category)->firstOrFail();
    
        // or if you need a collection instead
    
        $category = Category::where('slug', '=', $slug_category)->get();
    
        if(!$category->count()) {
            abort(404);
        }
    
        $categories = Article::with('category')->whereHas('category', function($q) use($slug_category) {
            $q->where('slug', '=', $slug_category);})
            ->get();
            
            $cat = Category::get();
                
            return view ('cdetail', compact('categories','category','cat'));
    }
    
    public function detail($slug_category, $slug){
        $cat = Category::get();
        $article =  $categories = Article::with('category')->whereHas('category', function($q) use($slug_category) {
            $q->where('slug', $slug_category);})
            ->where('slug', $slug)
            ->firstOrFail(); // here
    
        return view ('detail', compact('article','cat'));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search