laravel 8 中的 FormRequest 消息不显示消息

FormRequest message in laravel 8 not show messages

在我的 laravel 8 应用程序中,我尝试创建两个文件 CreateUserRequestupdateUserRequest 以使用个性化验证。

在方法 rules 中,我调用了我的模型变量 $createRules 但是当我发送我的表单时,这不发送,不显示消息,错误...没有。

我已附上 CreateUserRequest、我的 model 和控制器的 store 方法:

创建用户请求

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Models\User;

class CreateUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return User::$createRules;
    }
}

型号

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'surname',
        'experience',
        'skills',
        'education',
        'location',
        'profesion',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public static $createRules = [
        'name'       => 'required|string|max:191',
        'email'      => 'required|string|email|max:191|unique:users',
        'password'   => 'required|min:4|max:10|confirmed',
    ];

    public static $updateRules = [
        'name'  => 'required|string|max:191',
        'email' => 'required|string|email|max:191|unique:users',
    ];
}

控制器的 store 方法

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Models\Role;
use App\Models\User;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Str;
use Intervention\Image\ImageManagerStatic as Image;
use JamesDordoy\LaravelVueDatatable\Http\Resources\DataTableCollectionResource;
use Prettus\Validator\Exceptions\ValidatorException;

public function store(CreateUserRequest $request)
{
    try {
        $data = array(
            'name'      => $request->get('name'),
            'email'     => $request->get('email'),
            'password'  => password_hash($request->get('password'), PASSWORD_BCRYPT),
            'api_token' => Str::random(60),
        );

        $user = User::create($data);
        $roles = $request->get('roles');

        // We assign the Client's role
        if(empty($roles)) {
            $user->assignRole(2);
        } else {
            foreach($roles as $rol){
                $user->assignRole($rol);
            }
        }
    } catch (ValidatorException $e) {
        Flash::error($e->getMessage());
    }

    Flash::success('This is a message!');
    
    return redirect()->route('users.index');
}

blade 中,我有 @include('flash::message') 用于获取闪现的消息,并且此视图包含在其他 @include('admin.layouts.alertMessage') 中。

我不知道我做错了什么。

谢谢你的帮助,抱歉我的英语不好。

已更新

我的问题我认为我没有到达我的控制器。我用一个退出在我的函数中做 echo 并加载我的所有页面而没有消息 echo

附上我的表格和路线:

<form action="{{ route('users.store') }}" method="POST">
    @csrf
    @include('admin.users.fields')
</form>

路线

/** USERS BLOQ ROUTES */
Route::resource('users', UserController::class);

TBH,我不知道您可以从他们的模型中获取验证逻辑。我的建议是将您的验证逻辑复制粘贴到请求中。由于请求仅用于验证目的 ( CMIIW )

创建请求

public function rules()
    {
        return [
            'name'       => 'required|string|max:191',
            'email'      => 'required|string|email|max:191|unique:users',
            'password'   => 'required|min:4|max:10|confirmed',
        ];
    }

更新请求

public function rules()
        {
            return [
                'name'  => 'required|string|max:191',
                'email' => 'required|string|email|max:191|unique:users',
            ];
        }

更新

只需将其合并为一个请求,为 password 请求提供 RequiredIf,以防您的更新路由包含 update

use Illuminate\Validation\Rule;
...

public function rules()
{
    return [
          'password' => Rule::requiredIf(str_contains(url()->current(), 'update')) . '|min:4|max:10|confirmed',
    ]
}