簡體   English   中英

使用Laravel 4將多行插入數據庫表

[英]Inserting multiple rows into database table using laravel 4

我是Laravel 4的新手,所以請容忍我。 我想將從create.blade.php文件中獲得的數據store()到控制器的store()方法中。 但是我對控制器一無所知。

這是我的create.blade.php文件

<p>
    <strong>Select No of Activites You Want To Fill Up:</strong>&nbsp;{{ Form:: select('noAct',array('1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5),'1',array('id'=>'noAct')); }}</strong>
</p>

<table class="table table-bordered">
    <tr>
        <th>SN</th>
        <th>Activity Description</th>
        <th>Unit</th>
        <th>Weightage</th>
        <th>Date</th>
        <th>100</th>
        <th>75</th>
        <th>50</th>
        <th><50</th>
    </tr>
    <!--form to insert the user data-->
    {{ Form::open(array('url'=>'it')) }}

    <tbody id="tbodyappend"></tbody>
    <tbody>
        <tr>
            <td colspan="9" style="text-align:center;">{{ Form::submit('Add Activity',array('class'=>'btn btn-primary')) }}</td>
        </tr>
    </tbody>
    {{ Form::close() }}
</table>

<script>
    $(document).ready(function() {

        function displayTable() {
            var noAct = $("#noAct").val();

            $("#tbodyappend").empty();
            //for loop to display the no of tables required
            for (var i = 0; i < noAct; i++) {
                //display the table
                $("#tbodyappend").append("<tr><td>" + (i + 1) + "</td><td><input type='text' name='activity_name[]' required /></td><td><input type='text' name='activity_unit[]' required style='width:40px;' /></td><td><input type='text' name='activity_weight[]' required style='width:40px;' /></td><td><input type='text' name='activity_date[]' required style='width:40px;' /></td><td><input type='text' name='performance1[]' required style='width:40px;' /></td><td><input type='text' name='performance2[]' style='width:40px;' /></td> <td><input type='text' name='performance3[]' style='width:40px;' /></td><td><input type='text' name='performance4[]' style='width:40px;' /></td></tr>");

            }
        }


        $("select").change(displayTable);

    }); 
</script>

因此,現在我想將數據發送到控制器並插入表中。 因此,任何幫助都可能對我有很大幫助。

您可以在以下位置看到我想要制作的類似表格: http : //jsfiddle.net/xn2GP/1/

而且您可以看到我的粘貼: http : //laravel.io/bin/RWXK

編輯(實際答案):避免將<form>嵌套在<table> ,因為它在<form>內不呈現任何<input> <form> ,因此不發送任何數據

您需要將輸入保留在表單內。 假設我要存儲一個博客文章,並且我需要一個標題和正文:

{{ Form::open(array('url' => 'posts')) }}
    {{ Form::text('title') }}
    {{ Form::text('body') }}

    {{ Form::submit('Send') }}
{{ Form::close() }}

然后,我可以使用Input::get訪問值,並將其插入到posts表中,如下所示:

class PostsController extends \BaseController {

    public store()
    {
        // perform validation if needed

        $new_post = Post::create(array(
            'title' => Input::get('title'),
            'body' => Input::get('body')
        ));
    }
}

要么

class PostsController extends \BaseController {

    public store()
    {
        // perform validation if needed

        $new_post        = new Post;
        $new_post->title = Input::get('title');
        $new_post->body  = Input::get('body');

        $new_post->save();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM