gpt4 book ai didi

php - laravel 字段没有默认值

转载 作者:行者123 更新时间:2023-11-29 15:39:01 26 4
gpt4 key购买 nike

我这里有一个小问题,那就是图像字段没有编译者所说的默认值,但实际上我为其分配了一个值,我知道当该字段应该有一个值时会弹出此错误,但是程序员没有为其分配值,我知道所有这些,但我已经为我的字段分配了值,所以我在这里缺少什么

Controller

protected function validator(array $data)

{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'image' => ['required' , 'image' , 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
protected function create(array $data)
{

$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('images'), $imageName);

return User::create([
'name' => $data['name'],
'email' => $data['email'],
'image' => $imageName,
'password' => Hash::make($data['password']),
]);
}

查看

@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">{{ __('Register') }}</div>

<div class="card-body">
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
@csrf

<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>

<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>

<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>

<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>

<label class="col-md-4 col-form-label text-md-right" >Upload a picture</label>
<div class="custom-file mb-3 col-sm-6">
<input type="file" class="custom-file-input center" id="image" name="image">
<label class="custom-file-label" for="customFile"></label>
</div>

<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

问题主要来自 Controller ,但我说我放置 View 只是为了以防万一您想看到它

小注意事项:图像正在上传,它存储在公共(public)文件夹中,就像代码所说的那样,但它的名称不会保存在数据库中,字段图像也是我数据库中的字符串,因为我只想要它的名称,这样我就可以查看它

快照

the error message

最佳答案

为图像字段设置默认图像。另外,请确保您已将其设置为可填充,如果没有将其设置在模型 User.php

中的 protected $fillable

You will get this error when a user doesn't upload an image.

$table->string('image')->default('default.jpg');//You can also add .png

现在您的存储中有一个名为 default.jpg
的默认图像这样,如果用户不上传图像,图像将具有默认值。

关于php - laravel 字段没有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57882436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com