0

i've made a laravel newswebsite. i've tried to add auth by doing make:auth, but when i click the button login or register it gives me an error like this:

enter image description here

i dont know where it is coming from. it refers to a function in my controller but i dont know why cause it doesnt need that function at all. i usually dont have errors while adding authorization. is there anyone who's had the same problem or anyone who knows how to fix it?

app.blade.php

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link rel="stylesheet" href="{{asset('css/custom.css')}}">
    <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>        
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
    <title>{{config('app.name', 'News')}}</title>

</head>
<body>
    @include('inc.navbar')
    @include('inc.messages')
    @yield('content')
    @include('inc.footer')
</body>

navbar.blade.php

<header>
    <div class="container-fluid">
        <div class="row headercontent">
            <div class="col-md-3 offset-md-1 borderbottom">

            </div>
            <div class="col-md-4 borderbottom header-logo  bg-white">
                <a href="{{url('/')}}"><img class="high-pic"
                                            src="https://upload.wikimedia.org/wikipedia/commons/7/77/The_New_York_Times_logo.png"
                                            style="width: 100%;"></a>
            </div>

            <div class="col-md-3 search">
                <span class="fas fa-search"></span>
                <input type="text" class="borderbottom backgroundtextinput"
                       style="position:absolute; bottom:0; width:80%;outline:none;">

            </div>

        </div>
        <div class="row logo-NY">
            <div class="col-md-6 offset-md-3 borderbottom header-logo bg-white">
                <div style="z-index:3000;"></div>
                <a href="{{url('/')}}"><img class="high-pic"
                                            src="https://upload.wikimedia.org/wikipedia/commons/7/77/The_New_York_Times_logo.png"
                                            style="width: 100%;"></a>
            </div>
            <div class="col-md-3">
                <div id="clickme" class="d-lg-none">
                    <i class="fas fa-arrow-down"></i>
                </div>
            </div>
            <div class="col-md-3">

            </div>
        </div>
    </div>

    <div class="desktop-nav">
        <nav class="navbar navbar-expand-lg navbar-light font">

            <div class="container">

                <div class="" id="navbarSupportedContent" style="margin:auto;">
                    <div class="navbar-flex fontcolorblack">
                        @foreach($categories_navbar as $item)
                            <div class="nav-item">
                                <a class="nav-link bold" href="{{url($item->naam)}}">{{ucfirst($item->naam)}}</a>
                            </div>
                        @endforeach
                        @guest
                        <li class="nav-item">
                            <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                        </li>
                        @if (Route::has('register'))
                            <li class="nav-item">
                                <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                            </li>
                        @endif
                    @else
                        <li class="nav-item dropdown">
                            <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                {{ Auth::user()->name }} <span class="caret"></span>
                            </a>

                            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="{{ route('logout') }}"
                                   onclick="event.preventDefault();
                                                 document.getElementById('logout-form').submit();">
                                    {{ __('Logout') }}
                                </a>

                                <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                    @csrf
                                </form>
                            </div>
                        </li>
                    @endguest
                    </div>

                </div>
            </div>
        </nav>
    </div>

    <div id="mobile-nav" style="width: 100%; display: none; background-color: #2e3192; color: #fff;">
        <div id="closeme" class="d-lg-none">
            <i class="fas fa-times fa-2x"></i>
        </div>
        <nav class="navbar navbar-light bg-light fixed-top d-lg-none" style="z-index:999;">
            <ul class="navbar-nav mr-auto">
                <a href="{{url('/')}}"><img class="high-pic"
                     src="https://upload.wikimedia.org/wikipedia/commons/7/77/The_New_York_Times_logo.png"
                     style="width: 100%;"></a>
                @foreach($categories_navbar as $item)
                    <div class="nav-item">
                        <a class="nav-link bold" href="{{url($item->naam)}}">{{ucfirst($item->naam)}}</a>
                    </div>
                @endforeach

                <form class="form-inline my-2 my-lg-0" id="search_mobile" action="search" method="get">
                    <input class="form-control mr-sm-2" name="search" type="search" placeholder="Search..."
                           aria-label="Search">
                </form>

                @guest
                        <li class="nav-item">
                            <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                        </li>
                        @if (Route::has('register'))
                            <li class="nav-item">
                                <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                            </li>
                        @endif
                    @else
                        <li class="nav-item dropdown">
                            <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                {{ Auth::user()->name }} <span class="caret"></span>
                            </a>

                            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="{{ route('logout') }}"
                                   onclick="event.preventDefault();
                                                 document.getElementById('logout-form').submit();">
                                    {{ __('Logout') }}
                                </a>

                                <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                    @csrf
                                </form>
                            </div>
                        </li>
                    @endguest
            </ul>
        </nav>
    </div>

</header>
1
  • have you done any changes in Auth files Commented Mar 1, 2019 at 9:56

5 Answers 5

2

It seems to me that the problem actually is in the above line.

$category = Categorie::where('naam',$category)->first();

This line is not returning any data. Try putting dd($category) after this line to check if there is indeed a category with your query.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer btw, it returns 'null'. do you have any idea where i have to search for what to change
You need to check your request parameter and route
There are 2 variable with the same name ($category). One is passed as a variable. (I meant oversight($category) )... Also after querying you are putting the result in $category variable. This is a very bad practice.
1

Probably you are trying to access non-existing category, this page should return 404 error.

after $category = Categorie::where('naam',$category)->first();

add line

abort_if(is_null($category), 404);

and it will return 404 error as it must be.

2 Comments

thanks for the solution, it goes to 404 not found but it still doesnt go to the auth login page
I've added a new route to /login and /register in web.php and now everything works!
1

Try to debug like this,

public function oversight($category){
    try{
        $getCategory = Categorie::where('naam',$category)->first();
        if($getCategory !== null){
            dd("I GOT SELECTED CATEGORY ROW");
        }else{
            dd("I THINK THERE IS NO DATA AVAIALBLE BASED ON WHERE Condition");
        }
        dd("ALL FINE I AM GOING BACK TO VIEW WITH DATA");
    }catch(\Exception $e){
        dd("I GOT THIS ERROR : ".$e->getMessage());
    }
}

I'm sure you'll able to fix problem by this.

Comments

1

As Narbil Farhan stated, the problem is in the line:

$category = Categorie::where('naam',$category)->first();

You seem to get no result from the Categorie table. First problem can be that the table does not exist, note that Laravel searches for the table 'Categories' (Searches for model name + s). You should probably catch the error if there is no data returned, like Nevermind23 suggested.

if(isset($category->id) && $category->id != ""){
$artikels = Artikel::where('category_id', $category->id)->get()
}

Personal note, try to prevent programming in your native language. This is harder for non Dutch speakings to understand your code.

1 Comment

thanks for the solution. about the note, i know i didnt create it myself i gave my colleague the same critique
1

There are multiple solutions:

  1. Solution:
$category = Categorie::where('naam', $category)->first();
if(#category){
//you can get the $artikels belonging to the category
}

In this case, if there are no category with the respective name, $category will be null, and the code inside IF statement will not be executed.

  1. Solution:
$category = Categorie::where('naam', $category)->firstOrFail();
//you can get the $artikels belonging to the category

In this case, if the Category is not found, an Illuminate\Database\Eloquent\ModelNotFoundException will be thrown. If the exception is not caught, a 404 HTTP response is automatically sent back to the user.

  1. Solution: Route model binding - see the documentation
public function oversight(Categorie $category) {
    return view('pages.oversight', ['category'=>$category]);
}

In this case, if there is no category with the respective name/id, a 404 HTTP response is automatically sent back to the user.

Also, I'd suggest you, to create a( most probably OneToMany) relationship between the Categorie and Artikel model. It will make your life much simpler :)

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.