laravel UserRequest $request error
laravel5.2,I create a UserRequest.php under Requests directory,but in controller,public function add(UserRequest $request) show error,but use public function add(Request $request) is normal.
UserRequest
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'user_sn' => 'required|unique',
'user_name' => 'required',
'email' => 'required|unique',
'password' => 'required',
];
}
}
UserController
namespace App\Http\Controllers;
use App\Http\Requests\UserRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
public function add(UserRequest $request)
{
if ($request->get('dosubmit')) {
$validator = Validator::make($request->all(), $request
->rules(), $request->messages());
if ($validator->fails()) {
return redirect('user/add')->withErrors($validator)
->withInput();
}
}
$corporation_list = DB::table('corporation')->get();
$department_list = DB::table('department')->get();
return view('user.add', ['corporation_list' => $corporation_list, 'department_list' => $department_list]);
}
}
Route
Route::group(['middleware'],function (){
Route::any('user/add',['as'=>'user.add','uses'=>'UserController@add']);
});
There are usually 2 reasons you could be having this issue.
-
You've not added the
usestatement for theUserRequest.At the top of your controller (above the
class) add:use App\Http\Requests\UserRequestassuming that is the correct namespace.
- You may need to run
composer dump-autoloadto make sure the class has been added to the autoloader.
Edit
Firstly, replace the add() method with the following methods:
public function create()
{
$corporation_list = DB::table('corporation')->get();
$department_list = DB::table('department')->get();
return view('user.add', compact('corporation_list', 'department_list'));
}
public function store(UserRequest $request)
{
// If you get to this point the validation will have passed
// Process the request
}
Then change your routes from:
Route::any('user/add',['as'=>'user.add','uses'=>'UserController@add'])
to:
Route::get('user/add', ['as' => 'user.add', 'uses' => 'UserController@create']);
Route::post('user/add', ['as' => 'user.store', 'uses' => 'UserController@store']);
Obviously, feel free to change the as in the Routes to whatever, they should unique though.
Lastly, I would suggest looking at Resource Controllers which is a RESTful approach.
Hope this helps!
-
your issue1 I have already use this statement,so I tried issue 2,but problem is still,this problem appears when I use iframe in my project ,when I click the left menu,the right show the "welcome page" not the correct page.but when I use Request $request,the right show the right page – zahdxj Aug 30 '17 at 7:15
-
@zahdxj Would you be able to add the code for the
UserRequestto your question? – Ross Wilson Aug 30 '17 at 7:26 -
I have posted my UserRequest – zahdxj Aug 30 '17 at 7:51
-
thanks a lot,I just study laravel for a few days,you are a patient person... – zahdxj Aug 30 '17 at 8:34
-
1I'm a chinese,my english is poor,It's a little exhausting.... – zahdxj Aug 30 '17 at 9:03
The problem is that you have not identified UserController that you are using UserRequest file
use App\Http\Requests\UserRequest
It will solve the problem

浙公网安备 33010602011771号
usestatement at top of file forUserRequest! Like:use App\Http\Requests\UserRequest;– Hiren Gohel Aug 30 '17 at 6:54add()method use to show the form or process the form submission or both? – Ross Wilson Aug 30 '17 at 7:58Routes and theadd()method to your question as well I'll be able to help you out. – Ross Wilson Aug 30 '17 at 8:03