2

I am trying to edit a row in my database. I try to load the edit form with the content in it as below but i seem to get an error: "Some mandatory parameters are missing ("matches") to generate a URL for route "matches.update". "

@extends('master')
    @section('content')
        <h1>Compare Ages</h1>   

        {{ Form::open(array('action' => 'MatchesController@update')) }}


            {{ Form::label('n1label', 'Person 1: ') }}
            {{ Form::text('name1', $match->name1) }}<br/>
            {{ Form::label('v1label', 'Age: ') }}
            {{ Form::text('val1', $match->val1) }}<br/><br/>

            {{ Form::label('n2label', 'Person 2: ') }}
            {{ Form::text('name2', $match->name2) }}<br/>
            {{ Form::label('v2label', 'Age: ') }}
            {{ Form::text('val2', $match->val2) }}<br/><br/>

        {{ Form::submit('Update Pair') }}

        {{ Form::close() }}

    @stop

This is what I have on my controller for the edit and the update method:

public function edit($id)
{
    print_r(Mydata::find($id));
    return View::make('matches.edit')

        ->with('title', 'Edit Match')
        ->with('match', Mydata::find($id));

}

public function update($id)
{

    $input = Mydata::where('id', Mydata::find($id));
    $new_input = array(
        'name1'=>Input::get('name1'),
        'val1'=>Input::get('val1'),
        'name2'=>Input::get('name2'),
        'val2'=>Input::get('val2')
        );
    $input->update($new_input);


    return Redirect::to('matches')
    ->with('message', 'Match updated successfully!');

}

Please let me know how i can load the content on matches/edit form and save after editing using route matches/update then get redirected to matches/$id with the updated data

3 Answers 3

3

I think this has to do with your function requiring the ID. I imagine you need to include the ID of the item being updated with your form open call.

I'm not sure of the syntax that you'll need, perhaps something like what Taylor mentions here: https://github.com/laravel/framework/issues/844#issuecomment-15996265

or what they say here: Laravel 4: What to pass as parameters to the Url class?. (this is of course for the URL class but I imagine it'll be similar for the Form class)

Edit:

Ok, here's the code from the Form's 'getControllerAction' function which is called by Form::open if you use the 'action' parameter.

/**
 * Get the action for an "action" option.
 *
 * @param  array|string  $options
 * @return string
 */
protected function getControllerAction($options)
{
    if (is_array($options))
    {
        return $this->url->action($options[0], array_slice($options, 1));
    }

    return $this->url->action($options);
}

So it looks like it just grabs the first element of the passed array for the controller action, and then uses the rest as options. Therefore you should just be able to go:

{{ Form::open(array('action' => 'MatchesController@update', 'id' => $match->id)) }}

Good luck :)

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

Comments

1

Just a little tweak: if you change

{{ Form::open(array('route' => array('matches.update', $match->id), 'method' => 'PUT')) }}

with

{{ Form::model($match, array('route' => array('matches.update', $match->id), 'method' => 'PUT')) }}

Laravel will populate the existing fields on the form; a nice convenience for forms which need to be edited. Note the use of Form::model passing the parameter $match.

Comments

0

I was able to view the form, edit, submit and make changes to the database by altering my code this way: On the edit.blade.php form:

{{ Form::open(array('route' => array('matches.update', $match->id), 'method' => 'PUT')) }}

On the MatchesController.php controller:

public function update($id)
{
    $input = Mydata::find($id);
    $input->update(array(
        'name1'=>Input::get('name1'),
        'val1'=>Input::get('val1'),
        'name2'=>Input::get('name2'),
        'val2'=>Input::get('val2'),
        'updated_at'=>new DateTime
        ));
    $input->save();

    return Redirect::to('matches/'.$id)
    ->with('message', 'Match updated successfully!');

}

And finally, as you can see, i am able to redirect to the show view. Thank you, John!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.