我正在我的ProfileController.php中开发一个更新配置文件详细信息的函数。我正在验证请求,确保不需要任何内容,但是当我使用filled()函数时:
if($request->filled('username')){
$user->username = $request['username'];
}
if($request->filled('email')){
$user->email = $request['email'];
}
if($request->filled('profile_description')){
$user->profile_description = $request['profile_description'];
}
这不起作用,并且数据库将仅在所有字段都已填充时更新,但个人资料图片除外:
if($request->hasFile('file')){
if ($request['file']->isValid()) {
$file = $request['file'];
$destination = 'images/profile_pictures'.'/';
$ext= $file->getClientOriginalExtension();
$mainFilename = $user->username;
$user->pfp_file_extension = $ext;
// check if user has existing pfp
if (File::exists($destination, $mainFilename.".".$user->pfp_file_extension)) {
File::delete($destination, $mainFilename.".".$user->pfp_file_extension);
}
$file->move($destination, $mainFilename.".".$ext);
}
}
由于某种原因,hasFile()正在工作,因为它允许我不上传图片。
我读过Laravel的文档,它说“验证下的字段在存在时不能为空”,这并没有解释为什么它不工作。
<form method="POST" enctype="multipart/form-data">
@csrf
<h6 class="heading-small text-muted mb-4">User information</h6>
<div class="pl-lg-4">
<div class="row">
<div class="col-lg-6">
<div class="form-group focused">
<label class="form-control-label">Username</label>
<input type="text" id="username" name="username" class="form-control form-control-alternative" placeholder="{{ Auth::user()->username }}">
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-control-label">Email address</label>
<input type="email" id="email" name="email" class="form-control form-control-alternative" placeholder="{{ Auth::user()->email }}">
</div>
</div>
<div class="col-lg-6">
<div class="form-group focused">
<label class="form-control-label">Profile Picture</label>
<input type="file" class="file" name="file" id="file" accept=".png, .jpg, .jpeg">
</div>
</div>
<div class="row">
</div>
</div>
<hr class="my-4">
<!-- Description -->
<h6 class="heading-small text-muted mb-4">About me</h6>
<div class="pl-lg-4">
<div class="form-group focused">
<label>About Me</label>
<textarea id="profile_description" name="profile_description" rows="4" class="form-control form-control-alternative" placeholder="{{ Auth::user()->profile_description }}"></textarea>
</div>
</div>
<div>
<button class="btn btn-sm btn-primary" type="submit">Update Profile</button>
</div>
</form>
另外,这里是我的ProfileController.php的全部内容。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
class ProfileController extends Controller
{
public function index()
{
return view('profile');
}
public function profileUpdate(Request $request){
//validation rules
$request->validate([
'username' =>'min:4|unique:users,username|string|max:255',
'email'=>'unique:users,email|email:filter|max:255',
'profile_description'=>'string|max:10000',
'file'=> 'max:10000'
]);
$user = Auth::user();
if($request->filled('username')){
$user->username = $request['username'];
}
if($request->filled('email')){
$user->email = $request['email'];
}
if($request->filled('profile_description')){
$user->profile_description = $request['profile_description'];
}
if($request->hasFile('file')){
if ($request['file']->isValid()) {
$file = $request['file'];
$destination = 'images/profile_pictures'.'/';
$ext= $file->getClientOriginalExtension();
$mainFilename = $user->username;
$user->pfp_file_extension = $ext;
// check if user has existing pfp
if (File::exists($destination, $mainFilename.".".$user->pfp_file_extension)) {
File::delete($destination, $mainFilename.".".$user->pfp_file_extension);
}
$file->move($destination, $mainFilename.".".$ext);
}
}
$user->save();
return back()->with('message','Profile Updated');
}
}
先谢谢你。
2条答案
按热度按时间4ngedf3f1#
这是因为你不应该用这种方式获取
$request
变量的属性,就像它是一个普通的数组一样。$request->fill
可能工作正常,但属性不工作。您应该这样做:
$user->email = $request->input('email');
有关详细信息,请查看DOCS:https://laravel.com/docs/10.x/requests#retrieving-an-input-value
at0kjp5o2#
更新
已找到解决方案,在请求验证中,密钥必须具有规则“nullable”,例如