laravel 验证不适用于 Auth

laravel validation does not work with Auth

我正在尝试在控制器中添加验证,但它不适用于 auth:check。这是 Laravel 版本 5.7。

这是验证有问题的函数存储。

 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');
    }

这是错误信息:

[![在此处输入图片描述][2]][2]

更新 验证现在有效,但它产生了一个新问题。当我保存一个新玩家时,它会显示这个错误。

我认为问题出在这段代码

@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

您的大部分代码都是不必要的,您可以 protect the route 并删除大部分代码:

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');
}

所以它看起来像这样:

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');
}