Maison >développement back-end >tutoriel php >Laravel: 这个调研问卷的demo的控制器保存方法怎么写?
用的laravel5.2框架,下面这个调研问卷的控制器怎么写?demo如下:
数据表有3张,1和2多对多关系:
1、respondents //受访者
2、sports //喜欢的运动
3、sport_respondent //关联表
respondents
<code>id name email </code>
sports
<code>id sport_name 1 Basketball 2 Football 3 Table tennis 4 Badminton 5 Baseball 6 Swimming</code>
sport_respondent
<code>id respondent_id sport_id</code>
视图:
<code> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <link href="//cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> <link href="//cdn.bootcss.com/tether/1.3.2/css/tether.min.css" rel="stylesheet"> <div class="container"> <form> <fieldset class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder=""> </fieldset> <fieldset class="form-group"> <label for="email">Email</label> <input type="email" class="form-control" id="email" name="email" placeholder=""> </fieldset> <div class="checkbox row"> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport1" name="sport[]" value="1">Basketball </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport2" name="sport[]" value="2">Football </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport3" name="sport[]" value="3">Table tennis </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport4" name="sport[]" value="4">Badminton </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport5" name="sport[]" value="5">Baseball </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport6" name="sport[]" value="6">Swimming </label> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> <script src="//cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script> <script src="//cdn.bootcss.com/tether/1.3.2/js/tether.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script> </code>
问题:控制器怎么写?
<code>public function store(Request $request) { $respondent= new Respondent; $respondent->name = $request->name; $respondent->email = $request->email; $respondent->save(); $sport = new Sport; //接下来怎么写? return redirect()->action('RespondentController@index'); }</code>
用的laravel5.2框架,下面这个调研问卷的控制器怎么写?demo如下:
数据表有3张,1和2多对多关系:
1、respondents //受访者
2、sports //喜欢的运动
3、sport_respondent //关联表
respondents
<code>id name email </code>
sports
<code>id sport_name 1 Basketball 2 Football 3 Table tennis 4 Badminton 5 Baseball 6 Swimming</code>
sport_respondent
<code>id respondent_id sport_id</code>
视图:
<code> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <link href="//cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> <link href="//cdn.bootcss.com/tether/1.3.2/css/tether.min.css" rel="stylesheet"> <div class="container"> <form> <fieldset class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder=""> </fieldset> <fieldset class="form-group"> <label for="email">Email</label> <input type="email" class="form-control" id="email" name="email" placeholder=""> </fieldset> <div class="checkbox row"> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport1" name="sport[]" value="1">Basketball </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport2" name="sport[]" value="2">Football </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport3" name="sport[]" value="3">Table tennis </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport4" name="sport[]" value="4">Badminton </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport5" name="sport[]" value="5">Baseball </label> <label class="checkbox-inline col-xs-3"> <input type="checkbox" id="sport6" name="sport[]" value="6">Swimming </label> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> <script src="//cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script> <script src="//cdn.bootcss.com/tether/1.3.2/js/tether.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script> </code>
问题:控制器怎么写?
<code>public function store(Request $request) { $respondent= new Respondent; $respondent->name = $request->name; $respondent->email = $request->email; $respondent->save(); $sport = new Sport; //接下来怎么写? return redirect()->action('RespondentController@index'); }</code>
使用laravel的多对多关联模型插入方法。
获取受访者(respondents)的id:1)如果有登录,应该从session中获得;2)如果是即时创建的话,那创建就行了。说一下第二种方法
respondents与sports要建立多对多的关联关系。
<code class="php">public function store(Request $request) { $params = $request->all(); $respondent= new Respondent; $respondent->name = $params['name']; $respondent->email = $params['email']; $respondent->save(); $respondent->sports()->attach($params['sportd_ids']); }</code>
没用过Laravel
,不过像这种关系的保存数据,逻辑通的话还是很好写的;
先插入这张respondents
用户表,获取到插入的id
并保存;
然后后台接收到sports
的值是一个数组,然后以sports
数组foreach
,用刚才插入的用户id
和喜欢的运动value
值组合成sql
语句循环插入到sport_respondent
中
以上。