首页 > 解决方案 > Laravel 请求总是返回 null

问题描述

所以我有这个观点

<form method="POST" action="/store">
<div class="control-group after-add-more">  
<div class="form-group">                                                                    
<label>Partner :</label>                                                                        
<input type="text" class="form-control" id="partner" name="partner" value="" placeholder="Partner Name" />                                                                  
</div>                                                          
<button class="btn btn-success add-more" type="button">                                                         
<i class="glyphicon glyphicon-plus"></i> Plus Partner</button>                                                      
                                            
<div class="copy hide">                                                         
<div class="control-group">                                                                     
<div class="form-group">                                                                        
<label> Partner :</label>                                                                       
<input type="text" class="form-control" id="partner2" name="partner2" value="" placeholder="Partner Name" />                                                                    
</div>                                                                                                                              
<button class="btn btn-danger remove" type="button">                                                                    
<i class="glyphicon glyphicon-remove"></i>                                                          
Delete Partner</button>
</div>      
                                            
</div>
<button type="submit">Submit</button>
</form>

这是我使用的 jquery

$(document).ready(function() {
         $(".add-more").click(function() {
            var html = $(".copy").html();
            $(".after-add-more").after(html);
        });
        $("body").on("click", ".remove",function() {
            $(this).parents(".control-group").remove();
        });
});

但是当我尝试提交时,输入 partner2 正在返回空值

这是我的控制器

$kerma = Kerma::find(1)->get();
$kerma->partner()->createMany(['partner' => $request->input('partner')],['partner' => $request->input('partner2')])

有人对我有解决方案吗?提前致谢

标签: phplaravel

解决方案


向表单提交您的请求时:

  1. 路由文件应该POST配置了方法。
Route::post('store', 'YourController@yourmethod');
  1. 将 CSRF 令牌添加到表单以防止Page Expired问题。因此,您的刀片文件应如下所示:
<form method="POST" action="{{url('store')}}">
@csrf
<div class="control-group after-add-more">  
<div class="form-group">                                                                    
<label>Partner :</label>                                                                        
<input type="text" class="form-control" id="partner" name="partner" value="" placeholder="Partner Name" />                                                                  
</div>                                                          
<button class="btn btn-success add-more" type="button">                                                         
<i class="glyphicon glyphicon-plus"></i> Plus Partner</button>                                                      
                                            
<div class="copy hide">                                                         
<div class="control-group">                                                                     
<div class="form-group">                                                                        
<label> Partner :</label>                                                                       
<input type="text" class="form-control" id="partner2" name="partner2" value="" placeholder="Partner Name" />                                                                    
</div>                                                                                                                              
<button class="btn btn-danger remove" type="button">                                                                    
<i class="glyphicon glyphicon-remove"></i>                                                          
Delete Partner</button>
</div>      
                                            
</div>
<button type="submit">Submit</button>
</form>

推荐阅读