Question

I'm creating an edit page for my listings, and for some reason when I goto listings/view/{id}/edit I get redirected to listings/view/{id}

This is getEdit() method in ListingsController.php

public function getEdit($id)
    {
    $listings = Listing::find($id);

    $this->layout->content = View::make('listings/edit')
        ->with('listings', $listings);
    }

This is views/listings/edit.blade.php

<h1>Editing {{ $listings->id }}</h1>

<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::model($listings, array('route' => array('listings/edit', $listings->id), 'method' => 'PUT')) }}

    <div class="form-group">
        {{ Form::label('status', 'Status') }}
        {{ Form::email('status', null, array('class' => 'form-control')) }}
    </div>

    <div class="form-group">
        {{ Form::label('listingfor', 'For') }}
                {{ Form::select('listingfor', array(
                    ''          => 'Listing For',
                    'For Sale'     => 'For Sale',
                    'For Rent'     => 'For Rent',
                    'Sale-Rent'     => 'Sale - Rent'
                ),'',
                    array('class' => 'form-control'
                )) }}
    </div>

    {{ Form::submit('Edit', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

and this is routes.php

Route::controller('users', 'UsersController');
Route::controller('listings', 'ListingsController');
Route::post('listings/add', array('uses' => 'ListingsController@setAdd'));

Route::get('contacts', function()
{
    return View::make('listings.add');
});
Route::get('listings/edit/{id}', array('uses' => 'ListingsController@getEdit'));
Route::get('listings/view/{id}', array('uses' => 'ListingsController@getView'));

Route::get('add', array('uses' => 'ListingsController@getAdd'));
Route::controller('/', 'HomeController');

Routes from artisan routes

+--------+-----------------------------------------------------------------+------+----------------------------------+----------------+---------------+
| Domain | URI                                                             | Name | Action                           | Before Filters | After Filters |
+--------+-----------------------------------------------------------------+------+----------------------------------+----------------+---------------+
|        | GET|HEAD users/register/{one?}/{two?}/{three?}/{four?}/{five?}  |      | UsersController@getRegister      |                |               |
|        | GET|HEAD users/login/{one?}/{two?}/{three?}/{four?}/{five?}     |      | UsersController@getLogin         |                |               |
|        | GET|HEAD users/logout/{one?}/{two?}/{three?}/{four?}/{five?}    |      | UsersController@getLogout        |                |               |
|        | GET|HEAD users/dashboard/{one?}/{two?}/{three?}/{four?}/{five?} |      | UsersController@getDashboard     |                |               |
|        | POST users/create/{one?}/{two?}/{three?}/{four?}/{five?}        |      | UsersController@postCreate       |                |               |
|        | POST users/signin/{one?}/{two?}/{three?}/{four?}/{five?}        |      | UsersController@postSignin       |                |               |
|        | GET|HEAD|POST|PUT|PATCH|DELETE users/{_missing}                 |      | UsersController@missingMethod    |                |               |
|        | GET|HEAD listings/main/{one?}/{two?}/{three?}/{four?}/{five?}   |      | ListingsController@getMain       |                |               |
|        | GET|HEAD listings/view/{one?}/{two?}/{three?}/{four?}/{five?}   |      | ListingsController@getView       |                |               |
|        | GET|HEAD listings/add/{one?}/{two?}/{three?}/{four?}/{five?}    |      | ListingsController@getAdd        |                |               |
|        | GET|HEAD listings/edit/{one?}/{two?}/{three?}/{four?}/{five?}   |      | ListingsController@getEdit       |                |               |
|        | POST listings/edit/{one?}/{two?}/{three?}/{four?}/{five?}       |      | ListingsController@postEdit      |                |               |
|        | POST listings/add/{one?}/{two?}/{three?}/{four?}/{five?}        |      | ListingsController@postAdd       |                |               |
|        | GET|HEAD|POST|PUT|PATCH|DELETE listings/{_missing}              |      | ListingsController@missingMethod |                |               |
|        | POST listings/add                                               |      | ListingsController@setAdd        |                |               |
|        | GET|HEAD contacts                                               |      | Closure                          |                |               |
|        | GET|HEAD listings/edit/{id}                                     |      | ListingsController@getEdit       |                |               |
|        | GET|HEAD listings/view/{id}                                     |      | ListingsController@getView       |                |               |
|        | GET|HEAD add                                                    |      | ListingsController@getAdd        |                |               |
|        | GET|HEAD|POST|PUT|PATCH|DELETE {_missing}                       |      | HomeController@missingMethod     |                |               |
+--------+-----------------------------------------------------------------+------+----------------------------------+----------------+---------------+
Was it helpful?

Solution

You are manually calling listings/edit route but instead you should use built in function for this, so this:

{{ Form::model($listings, array('route' => array('listings/edit', $listings->id)...

should be like this:

{{ Form::model($listings, array('action' => array('ListingsController@edit', $listings->id)...

This way Laravel knows what you want.

OTHER TIPS

you have it already by default:

GET|HEAD listings/view/{one?}/{two?}/{three?}/{four?}/{five?}

so remove these two:

Route::get('listings/edit/{id}', array('uses' => 'ListingsController@getEdit'));
Route::get('listings/view/{id}', array('uses' => 'ListingsController@getView'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top