我已经为编辑用户设置了EditUsersRequest请求,并且为了确保电子邮件地址保持唯一但忽略了正在编辑的用户的电子邮件地址(因此他们不必将其电子邮件地址更改为某些内容每次编辑时都是唯一的),我在rules()函数中作为参数Request $ request。这昨天工作得很好,我没有对代码进行任何更改,但现在我不断收到错误Target [App \ Http \ Requests \ Request]不可实例化。在尝试更新用户时。我怎样才能解决这个问题?我不明白为什么它现在不起作用。这是请求和控制器的代码:
请求:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;
class EditUserRequest extends Request
{
public function authorize()
{
return Auth::user()->isActive() ? true:false;
}
public function messages()
{
return [
'unique'=>'That email address is already registered',
'regex'=>'Password must contain at least 1 letter and 1 number'
];
}
public function rules(Request $request)
{
return [
'name'=>'required|min:2|max:50',
'email'=>'required|email|max:50|unique:users,email,'.$request->get('id'),
'password'=>'required'
//'password'=>'required|min:8|regex:/[a-zA-Z][0-9]/' NOT IN USE WHILE IN DE
];
}
}
控制器方法:
public function update(EditUserRequest $request, $id)
{
$user = User::findOrFail($id);
if(trim($request->password) == '') {
$data = $request->except('password');
} else {
$data = $request->all();
$data['password'] = bcrypt($request->password);
}
if($file = $request->file('photo_id')) {
$name = time().'_'.$file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['path'=>$name]);
$data['photo_id'] = $photo->id;
}
$data['is_active'] = !$request->is_active ? 0:1;
$message = $user->update($data) ? "User {$user->name} has been updated": "Unable to update user {$user->name}";
Session::flash('updated', $message);
return redirect('/admin/users');
}
答案 0 :(得分:2)
使用
use Illuminate\Http\Request;
而不是
use App\Http\Requests\Request;
您也可以在此处查看:https://laravel.com/api/5.0/Illuminate/Http/Request.html
答案 1 :(得分:0)
尝试
use Request;
而不是
use App\Http\Requests\Request;
答案 2 :(得分:0)
$this->route('users')
可用于获取用户ID。这个或$this->segment(3)
可以正常工作。使用路线只是更有活力。