我正在Laravel 5.8中迈出第一步。从Laracast教程开始,我使用了现成的标准Laravel应用程序项目和用户身份验证,因此这里没有什么花哨的地方。用户登录后,他可以访问仪表板并更改一些自己的用户信息,这些信息存储在MySQL的Users表中。
一切正常,除了通过变量mediator_cert
上传文件之外。在检查用户是否已添加要上传的文件时(即mediator_cert
不应为null),我收到错误Call to undefined method konsens24\User::hasFile()
,即Laravel期望在用户中定义该方法.php文件未包含在其中(如果必须在其中明确包含它,我会感到惊讶吗?)。
我在线上查看了各种问题描述,特别是关于通过“ use”命令包含哪些问题的描述,但不幸的是没有结果。
User.php文件:
<?php
namespace konsens24;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'first_name', 'last_name','name', 'email', 'password', 'street', 'postal', 'city', 'state', 'country', 'phone', 'mediator', 'mediator_cert'
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function projects()
{
return $this->hasMany(Project::class, 'owner_id');
}
}
ProfileController.php文件:
<?php
namespace konsens24\Http\Controllers;
use Illuminate\Http\Request;
use konsens24\User;
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(User $user)
{
$user = auth()->user();
return view('profile.edit', compact('user'));
}
public function update(User $user)
{
abort_if($user->id !== auth()->id(), 403);
//Handle File Upload
if($user->hasFile('mediator_cert')){
//Get filename with the extension
$filenameWithExt = $user->file('mediator_cert')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get just ext
$extension = $user->file('mediator_cert')->guessClientExtension();
//FileName to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $user->file('mediator_cert')->storeAs('public/certs/', $fileNameToStore);
}
$user->update($this->validateUser());
return view('profile.edit', compact('user'));
}
protected function validateUser()
{
return request()->validate([
'street' => ['required', 'min:3', 'max:30'],
'postal' => ['required', 'min:3', 'max:7'],
'city' => ['required', 'min:3', 'max:30'],
'state' => ['max:30'],
'country' => ['required', 'min:1', 'max:30'],
'phone' => ['required', 'min:9', 'max:30'],
'mediator' => ['boolean'],
'mediator_cert' => ['nullable']
]);
}
}
edit.blade.php文件:
@extends('layouts.app')
@section('content')
<h1 class="title">Profile Settings</h1>
<p>Please complete your profile by adding the following information:</p>
<form method="post" action="/profile/{{ $user->id }}" enctype="multipart/form-data">
@method('PATCH')
@csrf
...
<div class="field">
<label class="label" for="mediator_cert">Certification (upload as PDF, Word Document DOCX, or JPG)</label>
<div class="control">
<input type="file" name="mediator_cert">
</div>
</div>
<br>
<div class="field">
<div class="control">
<button type="submit" class="button">Update Profile</button>
</div>
</div>
@include('errors')
</form>
@endsection
预期结果应该是可以执行hasFile()方法。 非常感谢您帮助我完成这一任务!
答案 0 :(得分:0)
您必须将 \ Illuminate \ Http \ Request $ request 作为 ProfileController @ update
注入并在方法中使用 $ request-> hasFile()而不是 $ user-> hasFile() 还可以使用 $ request-> file()或 $ request-> get()来获取Post,Get Form的数据参数
祝你好运
答案 1 :(得分:0)
谢谢阿敏,它成功了!这是我更改的内容:
public function update(User $user, Request $request)
{
abort_if($user->id !== auth()->id(), 403);
//Handle File Upload
if($request->hasFile('mediator_cert')){
//Get filename with the extension
$filenameWithExt = $request->file('mediator_cert')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get just ext
$extension = $request->file('mediator_cert')->guessClientExtension();
//FileName to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $request->file('mediator_cert')->storeAs('public/certs', $fileNameToStore);
$user->mediator_cert = $fileNameToStore;
}
$user->update($this->validateUser());
return view('profile.edit', compact('user'));
}