d3bug3r
d3bug3r

Reputation: 2586

My form route in laravel 5 keep pointing at user

My form model in laravel keep pointing at http://localhost:8000/user/profile/account/17 no matter what changes I made. I have two routes, one for admin another for user.

Even when I am on admin mode, the form route keep pointing at user.

 {!! Form::model($user, ['method'=>'PATCH', 'action' => ['ProfileController@update',$user->user_id]]) !!}

These are my routes:

Route::group(['prefix' => 'admin', 'middleware' => 'auth:admin'], function()
{
    $a = 'admin.';
    Route::get('/', ['as' => $a . 'home', 'uses' => 'AdminController@getHome']);
    Route::get('/profile', ['as' => $a . 'profile', 'uses' => 'ProfileController@index']);
    Route::get('/products', ['as' => $a . 'products', 'uses' => 'ProfileController@products']);
    Route::get('/payment', ['as' => $a . 'products', 'uses' => 'ProfileController@payment']);
    Route::get('/profile/account/{id}', ['as' => $a . 'edit', 'uses' => 'ProfileController@edit']);
    Route::patch('/profile/account/{id}', ['as' => $a . 'edit', 'uses' => 'ProfileController@update']);

});

Route::group(['prefix' => 'user', 'middleware' => 'auth:user'], function()
{

        $a = 'user.';
        Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController@getHome']);
        Route::get('/profile', ['as' => $a . 'profile', 'uses' => 'ProfileController@index']);
        Route::get('/profile/account/{id}', ['as' => $a . 'edit', 'uses' => 'ProfileController@edit']);
        Route::Patch('/profile/account/{id}', ['as' => $a . 'edit', 'uses' => 'ProfileController@update']);



});

Both admin and user share same controller ProfileController. Thanks!

Upvotes: 0

Views: 43

Answers (1)

Mwaa Joseph
Mwaa Joseph

Reputation: 763

How about using the route instead of action.

Then have an if statement to check if your admin or user before opening the form using model.

@if(admin)
   {!! Form::model($user, ['method'=>'PATCH', 'route' => ['admin.edit', $user->id]]) !!}
@else
    {!! Form::model($user, ['method'=>'PATCH', 'route' => ['user.edit', $user->id]]) !!}

Not the cleanest but addresses the issue of url form should point to. Using admin as placeholder please expose your own variable.

Upvotes: 2

Related Questions