0

Trying to bind model to the form to call update function, but model is not found.

{{ Form::model($upload, array('url' => array('uploads/update', $upload->id), 'files' => true, 'method' => 'PATCH')) }}

Controller to get edit view

public function getEdit($id)
{
 $upload = $this->upload->find($id);

if (is_null($upload))
{
  return Redirect::to('uploads/alluploads');
}

 $this->layout->content = View::make('uploads.edit', compact('uploads'));
}

Controller to do the update

public function patchUpdate($id)
{
  $input = array_except(Input::all(), '_method');

  $v = Validator::make($input, Upload::$rules);

  if ($v->passes())
  {
    $upload = $this->upload->find($id);
    $upload->update($input);

    return Redirect::to('uploads/show', $id);
  }

  return Redirect::to('uploads/edit', $id)
   ->withInput()
   ->withErrors($v)
}

error i get

ErrorException
Undefined variable: upload (View: /www/authtest/app/views/uploads/edit.blade.php)
5
  • Where is your binding ? Did you bind the model in route ? Commented Feb 18, 2014 at 23:52
  • In routes I have Route::controller('uploads', 'UploadsController'); Commented Feb 18, 2014 at 23:53
  • Then you have to pass the model from the controller when loading/showing the form, how do you show the form, code ? Commented Feb 18, 2014 at 23:54
  • I updated my question above, I get the selected id and pass that to the edit view Commented Feb 19, 2014 at 0:01
  • 1
    Its PHP error processed by Laravel error handler. Its nothing related to Laravel actually. Answer below is good one. Commented Feb 19, 2014 at 0:06

1 Answer 1

1

If you are not binding the model in the route then pass it from the controller when showing/loading the form for editing, for example:

public function getEdit($id)
{
    $upload = $this->upload->find($id);
    if (is_null($upload))
    {
        return Redirect::to('uploads/alluploads');
    }
    $this->layout->content = View::make('uploads.edit', compact('upload')); //<--
}

Update: You should use compact('upload') not uploads because the variable is $upload.

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

4 Comments

It's probably because of if condition, probably $this->upload->find($id) returns null.
In the url I can see that the correct id is passed. Does that mean the is not returning null ?
Yes, it's not null then, remove the if condition and check if works.
I removed the if condition as well, something else has to create this empty page, I can't seem to find it right now. Thanks for you help anyway.

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.