cch
cch

Reputation: 3386

Laravel 4 - ErrorException Undefined variable

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)

Upvotes: 0

Views: 1887

Answers (1)

The Alpha
The Alpha

Reputation: 146191

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.

Upvotes: 1

Related Questions