1

I am new in Laravel.

I made controller, Model and views by way/generator by composer php artisan generate:scaffold cities and Its index page (Create and store method) Working well but I don't know what is the problem with update method.

This is my CitiesController method(Update):

public function update($id)
{
    $city = City::findOrFail($id);

    $validator = Validator::make($data = Input::all(), City::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $city->update($data);
    return Redirect::route('admin.cities.index');
}

This is my model (city):

class City extends \Eloquent {
   protected $primaryKey='city_id';

    public static $rules = [
         'name'     => 'required',
         'image'     => 'mimes:jpeg',
         'parent_id' => 'required',
         'name'      => 'required',
         'english_name'=>'unique:cities,english_name|required'

    ];
    protected $fillable = ['name', 'parent_id', 'english_name','population','phone_prefix','image'];
}

And this is my view (edit):

<ul>
    {{ Form::model($city,array('route'=>array('admin.cities.update',$city->id),'method'=>'PUT','files'=>true)) }}
    <!--Here I included my form-->
    @include('admin.forms._partial.formcity')
    <li>
        {{ Form::submit('submit') }}
    </li>
    {{ Form::close() }}
</ul>

And this is my Route:

Route::group(array('prefix'=>'admin','before'=>'Auth'),function(){
    Route::resource('cities', 'CitiesController');
});

When I click on submit button Laravel throwout this error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

note:My view works well. I think the problem is from controller method, and other methods of this controller like create and store works well too.

8
  • that problem is generated beacause you have a defective route declaration in your routes.php Commented Jan 16, 2015 at 13:46
  • please give us your routes.php Commented Jan 16, 2015 at 13:48
  • @ITroubs I added my route too Commented Jan 16, 2015 at 13:49
  • one route sais "admin.cities...." but the form has a route that points to "siteadmin.cities....." is that correct or should the form rather point to "admin.cities.update"? Commented Jan 16, 2015 at 13:52
  • 1
    On the command line under the laravel directory, try php artisan routes and make sure you are using the correct route name and there are no duplicate route names. Commented Jan 16, 2015 at 14:35

1 Answer 1

0

In your model you have protected $primaryKey='city_id'; but in your view you have:

{{ Form::model($city,array('route'=>array('admin.cities.update',$city->id),'method'=>'PUT','files'=>true)) }}

I mean $city->id should be $city->city_id

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.