Search code examples
laravellaravel-5laravel-5.7laravel-validation

laravel validation does not work with Auth


I am trying to add validation in the controller but it does not work with auth:check. This is the Laravel version 5.7.

This is the function store that has a problem with the validation.

 public function store(Request $request)
    {
        $request->validate([
                'first_name' => ['required'|'min:2'|'max:50']
                ]);
        
        if(Auth::check()){
            
            $player = Player::create([
                'first_name' => $request->input('fist_name'),
                'last_name' => $request->input('last_name'),
            ]);
 
 
            if($player){
                return redirect()->route('players.show', ['player'=> $player->id])
                ->with('success' , 'foo');
            }
 
        }
         
        return back()->withInput()->with('errors', 'Foo Error');
    }

This is the error message: enter image description here

[![enter image description here][2]][2]

Update The validation works now but it generated a new problem. When I save a new player it's shows me this error.

enter image description here

I think the problem is this code

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ $player->first_name }} {{ $player->last_name }} <span class="float-right"><a class="" href="{{ route('players.edit', $player->id)}}">
                            {{ __('Spieler bearbeiten') }}
                        </a></span></div>

                <div class="card-body">
                    @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                    @endif

                    <ul>
                        <li>Vorname: {{ $player->first_name }}</li>
                        <li>Nachname: {{ $player->last_name }}</li>
                        <li>Land: {{ $player->country }}</li>
                        <li>Bild: {{ $player->image }}</li>
                        <li>Grösse: {{ $player->size }}</li>
                        <li>Gewicht: {{ $player->weight }}</li>
                        <li>Alter: {{ $player->date_of_birth }}</li>


                    </ul>

                    <br>
                    <br>
                    <a href="{{ route('tournaments.show', $player->tournament_id) }}">Zurück</a>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Solution

  • Most of your code is unnecessary, you can protect the route and get rid of most of it:

    public function store(Request $request)
    {
        // Your validation is not correct and you are missing the last_name.
        // $request->validate() will return the validated data if 
        // the validation was successful
        $request->validate([
            'first_name' => ['required' | 'min:2' | 'max:50']
        ]);
    
        // After protecting the route we can get rid of the auth check
        if (Auth::check()) {
            $player = Player::create([
                // Since $request->validate returns the validated data, this is unncessary
                'first_name' => $request->input('fist_name'),
                'last_name' => $request->input('last_name'),
            ]);
    
            // It will either create the player or throw an exception,
            // this conditional seems unnecessary
            if ($player) {
                return redirect()
                    // If you setup your routes correctly you can pass the model to the route
                    // so ['player' => $player->id] is unnecessary
                    ->route('players.show', ['player' => $player->id])
                    ->with('success', 'foo');
            }
        }
    
        // This return seems pointless since it can never be reached
        // after protecting the route
        return back()->withInput()->with('errors', 'Foo Error');
    }
    

    So it would look something like this:

    public function __construct()
    {
        $this->middleware('auth');
    }
    
    /**
     * @param Request $request
     *
     * @return RedirectResponse
     */
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
        ]);
    
        $player = Player::create($validatedData);
    
        return redirect()
            ->route('players.show', $player)
            ->with('success', 'foo');
    }