Rumah > pembangunan bahagian belakang > tutorial php > Laravel: 这个调研问卷的demo的控制器保存方法怎么写?

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

WBOY
Lepaskan: 2016-06-06 20:07:55
asal
1094 orang telah melayarinya

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

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

respondents

<code>id   name   email
</code>
Salin selepas log masuk
Salin selepas log masuk

sports

<code>id   sport_name
1    Basketball
2    Football
3    Table tennis
4    Badminton
5    Baseball
6    Swimming</code>
Salin selepas log masuk
Salin selepas log masuk

sport_respondent

<code>id   respondent_id   sport_id</code>
Salin selepas log masuk
Salin selepas log masuk

视图:

<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>
Salin selepas log masuk
Salin selepas log masuk

问题:控制器怎么写?

<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>
Salin selepas log masuk
Salin selepas log masuk

回复内容:

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

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

respondents

<code>id   name   email
</code>
Salin selepas log masuk
Salin selepas log masuk

sports

<code>id   sport_name
1    Basketball
2    Football
3    Table tennis
4    Badminton
5    Baseball
6    Swimming</code>
Salin selepas log masuk
Salin selepas log masuk

sport_respondent

<code>id   respondent_id   sport_id</code>
Salin selepas log masuk
Salin selepas log masuk

视图:

<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>
Salin selepas log masuk
Salin selepas log masuk

问题:控制器怎么写?

<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>
Salin selepas log masuk
Salin selepas log masuk

使用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>
Salin selepas log masuk

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

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

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

以上。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan