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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?