Laravel: 这个调研问卷的demo的控制器保存方法怎么写?

WBOY
Release: 2016-06-06 20:07:55
Original
1062 people have browsed it

用的laravel5.2框架,下面这个调研问卷的控制器怎么写?demo如下:

数据表有3张,1和2多对多关系:
1、respondents //受访者
2、sports //喜欢的运动
3、sport_respondent //关联表

respondents

<code>id   name   email
</code>
Copy after login
Copy after login

sports

<code>id   sport_name
1    Basketball
2    Football
3    Table tennis
4    Badminton
5    Baseball
6    Swimming</code>
Copy after login
Copy after login

sport_respondent

<code>id   respondent_id   sport_id</code>
Copy after login
Copy after login

视图:

<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>
Copy after login
Copy after login

问题:控制器怎么写?

<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>
Copy after login
Copy after login

回复内容:

用的laravel5.2框架,下面这个调研问卷的控制器怎么写?demo如下:

数据表有3张,1和2多对多关系:
1、respondents //受访者
2、sports //喜欢的运动
3、sport_respondent //关联表

respondents

<code>id   name   email
</code>
Copy after login
Copy after login

sports

<code>id   sport_name
1    Basketball
2    Football
3    Table tennis
4    Badminton
5    Baseball
6    Swimming</code>
Copy after login
Copy after login

sport_respondent

<code>id   respondent_id   sport_id</code>
Copy after login
Copy after login

视图:

<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>
Copy after login
Copy after login

问题:控制器怎么写?

<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>
Copy after login
Copy after login

使用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>
Copy after login

没用过Laravel,不过像这种关系的保存数据,逻辑通的话还是很好写的;

  1. 先插入这张respondents用户表,获取到插入的id并保存;

  2. 然后后台接收到sports的值是一个数组,然后以sports数组foreach,用刚才插入的用户id喜欢的运动value值组合成sql语句循环插入到sport_respondent

以上。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template