doucuo4413 2013-07-16 16:27
浏览 89
已采纳

Laravel 4:从视图提交数据时未定义的变量错误

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.');
}
  • 写回答

1条回答 默认 最新

  • douduxia1551 2013-07-17 03:48
    关注

    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.');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog