php - 没有默认值的 laravel 字段
问题描述
我在这里有一个小问题,它是图像字段没有编译器所说的默认值,但实际上我为它分配了一个值我知道当字段应该有一个值但程序员没有时弹出这个错误'不给它赋值我知道所有这些但我已经给我的字段赋值了所以我在这里缺少什么
控制器
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
问题主要出在控制器上,但我说我放了视图以防万一你想看到它
小提示:图像正在上传,它存储在公共文件夹中,就像代码所说的那样,但它的名称没有保存在数据库中,字段图像也是我数据库中的字符串,因为我只想要它的名称,所以我可以查看它
快照
解决方案
为您的图像字段设置默认图像。还要确保您已使其可填充,如果未将其设置 protected $fillable
在您的模型中User.php
当用户不上传图片时,您将收到此错误。
$table->string('image')->default('default.jpg');//You can also add .png
现在在您的存储中有一个名为default.jpg
的默认图像,
这样如果用户不上传图像,图像将具有默认值。
推荐阅读
- windows - 从 powershell 卸载设备
- eclipse - DBeaver 内容辅助颜色
- python - 从应用于初始数据帧的多个条件构建数据帧:这种情况适用于 pandas 而不是 pyspark?
- sql - sql作业停止重复
- swift - Xcode:不尊重水平约束?
- java - Android AlertDialogs 未在特定活动中显示按钮
- reactjs - 使用 Reactjs 将道具传递给功能组件时,如果语句不起作用?
- eclipse - 硒元素不可交互错误,没有反馈
- unit-testing - 如何在多种配置中运行相同的 xunit.net 测试套件?
- xml - 如何使用 XSLT 根据条件删除 XML 节点?