dongyu7074 2015-11-10 08:56
浏览 347
已采纳

Laravel从控制器中获取Select值(它只是为我返回0)

I have run into a roadblock with getting the value of the Select.

Please have a look at my form:

{!! Form::open(array('action' => 'MasterController@datamaker')) !!}

{!! Form::select('Area', array('Science', 'Arts',)); !!}

{!! Form::input('date', 'Year') !!}

{!! Form::submit('Add') !!}

Here is my (simplified) method in the controller:

public function datamaker() {

    $input = Request::all();

    $Database = new Database;

    $Database -> Area = Request::get('Area');
    $Database -> Year = $input['Year'];

    $Database -> save();

    return (Request::get('Area'));  <----- This is returning "0" instead of the values declared in the form

}

I have checked other sources and the solutions they have provided have not worked. Whatever I have tried, it always just returns "0" instead of the values declared in the form.

Can anyone point me in the right direction?

Thanks!

  • 写回答

1条回答 默认 最新

  • doushi1996 2015-11-10 09:38
    关注

    The reason why you are getting 0 instead of Science is that Form component, when generating a select, uses array keys as values of available options and array values as string that is displayed in the select. You don't provide array keys, so the keys are 0 for Science and 1 for Arts.

    If you want to get Science/Arts as the value from Request::get('Area') you need to pass an array to Form::select() where values and keys are the same strings, e.g.

    Form::select('Area', ['Science' => 'Science', 'Arts' => 'Arts']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable