Shebs72
Shebs72

Reputation: 23

Argument 2 passed to App\Http\Controllers\ProfileController::store() must be an instance of App\Http\Requests\CreateProfileRequest, none given

I have been pulling my hair out with this error for a few hours now but cannot understand why it is doing it. I have done this before on Laravel but it just does not seem to want to work.

This is the method I am trying to get to work:

public function store($name, CreateProfileRequest $request)
{
    $user = User::whereName($name)->first();


    $house = new House();
    $house->first_line_address = $request->get('first_line_address');
    $house->city = $request->get('city');
    $house->postcode = $request->get('postcode');

    $user->house()->save($house);

    return redirect('/');

}

This is the form I am trying to get the data from:

{!! Form::model($user->house, ['route' => 'profile.store']) !!}

    <div class ="form-group">
        {!! Form::label('first_line_address', 'First line of address:') !!}
        {!! Form::text('first_line_address', null, ['class' => 'form-control'])!!}
    </div>

    <div class ="form-group">
        {!! Form::label('city', 'Town/City:') !!}
        {!! Form::text('city', null, ['class' => 'form-control'])!!}
    </div>

    <div class ="form-group">
        {!! Form::label('postcode', 'Postcode:') !!}
        {!! Form::text('postcode', null, ['class' => 'form-control'])!!}
    </div>

    <div class ="form-group">
        {!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}
    </div>

    {!! Form::close() !!}
</div>

It is fine until I attempt to store it then I get

Argument 2 passed to App\Http\Controllers\ProfileController::store() must be an instance of App\Http\Requests\CreateProfileRequest, none given

This is my route:

$router->resource('profile', 'ProfileController');

| GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController@index | |

| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController@create | |

| | POST | profile | profile.store | App\Http\Controllers\ProfileController@store | |

| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController@show | |

| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController@edit | |

| | PUT | profile/{profile} | profile.update | App\Http\Controllers\ProfileController@update | |

| | PATCH | profile/{profile} | | App\Http\Controllers\ProfileController@update | |

| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController@destroy | |

If anyone thinks of how to get rid of this ridiculous error I will be forever grateful :)

Upvotes: 2

Views: 14237

Answers (2)

lukasgeiter
lukasgeiter

Reputation: 152860

The store() method of a resource controller/route doesn't take any route parameters. That means Laravel has nothing to pass as $name argument. I suppose you're passing the name in the request data itself. If that's the case you can access it with $request->input('name').

Anyways you should remove $name from the method signature:

public function store(CreateProfileRequest $request)

Upvotes: 3

N.B.
N.B.

Reputation: 14060

If you want an instance of Request to be available to your controller method, it must be the first argument, followed by other arguments passed via URI. Reverse the methods.

Upvotes: 3

Related Questions