【问题标题】:Laravel 5.3 CLI stop working when using foreach on array request在数组请求上使用 foreach 时,Laravel 5.3 CLI 停止工作
【发布时间】:2016-12-12 18:06:39
【问题描述】:

我有这个表单结构

<form> <!-- assume I have method and action attribute in the form -->
    <fieldset>
        <input type="checkbox" name="settings[]" value="1" checked>
        <input type="checkbox" name="settings[]" value="2" checked>
        <input type="checkbox" name="settings[]" value="3" checked>
    </fieldset>
    <button>Save</button>
</form>

然后尝试捕获数组请求

//route
Route::post('/settings/save','SettingsController@save_settings');
//in controller
public function save_settings(Request $request){
    foreach($request->settings[] as $s){
        //save settings
    }
}

每次我点击表单按钮时,Laravel CLI 都会突然停止工作。

如果我删除“foreach”,一切正常,就像没有“CLI 停止工作”一样。有什么想法,请帮忙?我正在使用 Laravel 5.3、php 7、wamp。

【问题讨论】:

    标签: php laravel command-line-interface laravel-5.3


    【解决方案1】:

    用这个替换你的控制器功能。

    public function save_settings(Request $request)
    {
      foreach($request->get('settings') as $s) {
        //save settings
      }
    }
    

    请理解,要从 $request 获取任何输入,请使用 get()/all()/except()/only()/input() 函数:)

    文档提供here

    【讨论】:

    • 什么?在我的项目中,我可以毫无问题地执行 $request->theinputvalue 。无论是在获取还是发布中,我都可以执行 $request->theinputvalue。
    • @CodeDemon 你试过我的答案了吗?
    【解决方案2】:

    试试这个:

    public function save_settings(Request $request){
        $setting=Input::get('settings');
        $count=count($setting);
        if($count >0 ){
            foreach($setting as $s){
                //save settings
            }
        }
    }
    

    【讨论】:

    • foreach($setting as $s){ //保存设置 }
    • 你也可以用这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2018-01-08
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    相关资源
    最近更新 更多