Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何防止输入空值_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 如何防止输入空值

Php 如何防止输入空值,php,laravel,laravel-4,Php,Laravel,Laravel 4,我有一个简单的foreach循环,它检查每个选择的服务(来自html表单),然后通过Laravel4模型将信息保存到数据库中。该信息可以很好地保存,但即使为null,也会保存foreach输入。如果选择了数据,如何保存数据 控制器 public function getServices() { $user = User::find(Auth::user()->id); $input = [

我有一个简单的foreach循环,它检查每个选择的服务(来自html表单),然后通过Laravel4模型将信息保存到数据库中。该信息可以很好地保存,但即使为null,也会保存foreach输入。如果选择了数据,如何保存数据

控制器

  public function getServices() {
            $user = User::find(Auth::user()->id);   

            $input = [
                    'rooms' => Input::get('rooms'),
                    'pr_deodorizer' => Input::get('pr_deodorizer'),
                    'pr_protectant' => Input::get('pr_protectant'),
                    'pr_sanitizer' => Input::get('pr_sanitizer'),
                    'fr_couch' => Input::get('fr_couch'),
                    'fr_chair' => Input::get('fr_chair'),
                    'pr_sectional' => Input::get('pr_sectional'),
                    'pr_ottoman' => Input::get('pr_ottoman'),
                    'pr_tile' => Input::get('pr_tile'),
                    'pr_hardwood' => Input::get('pr_hardwood')
            ];

            $empty = '<i class="icon-warning-sign"></i> No services were selected. Need help? Contact us';                          

            if(empty($input['rooms']) && empty($input['pr_deodorizer']) && 
                    empty($input['pr_sanitizer']) && empty($input['pr_protectant']) 
                    && empty($input['fr_couch']) && empty($input['fr_chair'])
                    && empty($input['pr_sectional']) && empty($input['pr_ottoman'])
                    && empty($input['pr_tile']) && empty($input['pr_hardwood'])
                     ){
                return Redirect::to('book/services')->withErrors($empty)->withInput();
            } 

            foreach($input as $services)
            {
                $service = new Service();

                $service->userID = $user->id;
                $service->services = $services;

                $service->save();
            }
            return Redirect::to('book/schedule');
    }

服务专栏就是我所看到的。用户选择的服务的serviceID为3,但没有其他内容。但它占用了空行,这会导致报告错误。我怎样才能让它只保存它们实际填充的数据?Laravel 4验证是否可行?

我同意elclanrs的观点-代码看起来不正确

但要回答你的问题,你就不能这样做吗

            foreach($input as $services)
            {
                $service = new Service();

                $service->userID = $user->id;
                $service->services = $services;

                if ( ! (is_null($services->services)))
                {
                    $service->save();
                }
            }

它看起来不太像拉威尔。。。考虑关注点的分离。然后查看内置验证器帮助程序以验证数据。您可以使用
protected$guarded=[]
启用批量分配,然后从那里开始,逐个验证服务中的字段,并将其注入控制器。如果你遵循最佳实践,你的控制器不应该超过5-10行。不太清楚你的意思。L4没有任何类型的验证器来检查输入的任何字段(这就是为什么我只创建了一个简单的if(空))。如果我要清理阵列并再次对这些阵列进行检查,那么控制器将大大缩小。这仍然是一个开发功能。但这与我的问题无关。这只是对您的代码的观察,但我的观点是,您可以将在庞大的
语句中重复的逻辑抽象到使用验证器的服务中,然后您只需要一个
isset
。检查“扩展验证器类”的文档,好的,我会检查。我不是想粗鲁什么的。我仍在学习,不得不使用黑客代码来获得结果。然后我通常回去打扫卫生。如果您在循环中使用var_dump(%services->services),我将检查该页面-显示什么?也许您需要检查$services->services=“”-因为它是一个空字符串而不是null?无法var_dump$services->services,因为它不存在。var_dumping$service->services只返回一个值“3”
            foreach($input as $services)
            {
                $service = new Service();

                $service->userID = $user->id;
                $service->services = $services;

                if ( ! (is_null($services->services)))
                {
                    $service->save();
                }
            }