laravel UserRequest $request error

Ask Question   0  

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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']);

});

laravel  request share improve this question edited Aug 30 '17 at 8:27     asked  Aug 30 '17 at 6:53 laravel UserRequest $request error 随笔 第1张 zahdxj 33
  • 1 You need to add use statement at top of file for UserRequest! Like: use App\Http\Requests\UserRequest; – Hiren Gohel Aug 30 '17 at 6:54 
  •   Is you add() method use to show the form or process the form submission or both? – Ross Wilson Aug 30 '17 at 7:58
  •   yes ,both ,is the problem here? – zahdxj Aug 30 '17 at 8:02
  •   Yes. If you can add the Routes and the add() method to your question as well I'll be able to help you out. – Ross Wilson Aug 30 '17 at 8:03
  •   Route::any('user/add',['as'=>'user.add','uses'=>'UserController@add']) – zahdxj Aug 30 '17 at 8:05
show 3 more comments

2 Answers

active oldest votes 1  

There are usually 2 reasons you could be having this issue.

  1. You've not added the use statement for the UserRequest.

    At the top of your controller (above the class) add:

    use App\Http\Requests\UserRequest
    

    assuming that is the correct namespace.

  2. You may need to run composer dump-autoload to 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'=>'UserControl‌​ler@add']) 

to:

Route::get('user/add', ['as' => 'user.add', 'uses' => 'UserControl‌​ler@create']);
Route::post('user/add', ['as' => 'user.store', 'uses' => 'UserControl‌​ler@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!

share improve this answer edited Aug 30 '17 at 8:28     answered  Aug 30 '17 at 7:03 laravel UserRequest $request error 随笔 第2张 Ross Wilson 17.6k23043
  •   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 UserRequest to 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
  • 1 I'm a chinese,my english is poor,It's a little exhausting.... – zahdxj Aug 30 '17 at 9:03
show 3 more comments   0

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

share improve this answer answered  Aug 30 '17 at 7:05 laravel UserRequest $request error 随笔 第3张 lizeshakya
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄