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

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.');
}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试构建一个计时器应用程序 - 这个表单应该提交时间(它确实)和客户端名称 从数据库填充,它看起来像这样:

  {{Form :: open(array('action'=>'TasksController @ store','name'=  >'计时器'))}} 
 {{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()}} 
   
 
 

生成此页面的控制器如下所示:

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

我在提交表单时收到“未定义变量:客户端”。 我不明白为什么。

我做错了什么?

编辑:我的TasksController中的商店功能如下所示:

  public function store()
 {
 $ input = Input :: all();  
 $ v = Validator :: make($ input,Task :: $ rules); 
 if($ v-> passes())
 {
 $ this-> task-> create($ 输入); 
返回View :: make('tasks.index'); 
} 
返回View :: make('tasks.index')
  - > withInput()
  - > withErrors(  $ v)
  - > with('message','有验证错误。'); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题