> 백엔드 개발 > PHP 튜토리얼 > Laravel: 这个调研问卷的demo的控制器保存方法怎么写?

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

WBOY
풀어 주다: 2016-06-06 20:07:55
원래의
1097명이 탐색했습니다.

用的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,不过像这种关系的保存数据,逻辑通的话还是很好写的;

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

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

以上。

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿