0

I'm trying building a timer app - this form should submit the time (which it does) AND the client name as populated from the database, it looks like this:

{{ Form::open(array('action' => 'TasksController@store', 'name' => 'timer')) }}
    {{ Form::select('client', $client , Input::old('client')) }}
    {{ Form::hidden('duration', '', array('id' => 'val', 'name' => 'duration')) }}
    {{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}

My controller that generates this page looks like this:

public function index()
{
    $client = DB::table('clients')->orderBy('name', 'asc')->lists('name','id');
    return View::make('tasks.index', compact('task', 'client'));
}

I am getting a "Undefined variable: client" when I submit the form. I can't see why.

What am I doing wrong?

EDIT: the store function in my TasksController looks like this:

public function store()
{
    $input = Input::all();  
    $v = Validator::make($input, Task::$rules);
    if($v->passes())
    {
        $this->task->create($input);
        return View::make('tasks.index');
    }
    return View::make('tasks.index')
        ->withInput()
        ->withErrors($v)
        ->with('message', 'There were validation errors.');
}
3
  • 3
    Show us store method from TasksController - you say that's the point where execution fails Commented Jul 16, 2013 at 19:27
  • You wouldnt have a $client variable in your controller - you'd have Input::get('client') Commented Jul 17, 2013 at 1:19
  • @Andreyco, check my edit please - what is the best way to add the $client variable into my controller? Commented Jul 17, 2013 at 2:04

1 Answer 1

2

You are returning the View::make() from your store() function, which is not the 'resourceful' way of doing it.

Your view is expecting to have $client included in it - but because store() does not return a $client - the error is generated.

Assuming you are using a resourceful controller - your store function should look like this:

public function store()
{
    $input = Input::all();  
    $v = Validator::make($input, Task::$rules);
    if($v->passes())
    {
        $this->task->create($input);
        return Redirect::route('tasks.index');  // Let the index() function handle the view generation
    }
    return Redirect::back()  // Return back to where you came from and let that method handle the view generation
        ->withInput()
        ->withErrors($v)
        ->with('message', 'There were validation errors.');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that's amazing it's throwing it into the database, only one issue - I get the error: "Unable to generate a URL for the named route "tasks.index" as such route does not exist" my route looks like this: Route::resource('/', 'TasksController'); I've always had this issue with my laravel apps but never understood it. Sorry if this is a bit of a silly question.
it should be Route::resource('tasks', 'TasksController);

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.