2014-03-31 76 views
0

我是laravel 4的新手,所以請容忍我。我想將我從create.blade.php文件獲得的數據存儲到控制器的store()方法中。但是我在控制器部分變得無能爲力。使用laravel插入多行到數據庫表4

這裏是我的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

回答

0

EDIT(實際的答案):避免嵌套一個<form><table>,因爲它在<form>內部不會顯示<input>,因此不會發送數據

您需要將輸入保留在表單中。比方說,我想保存一個博客帖子,我會需要一個標題和正文:

{{ 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(); 
    } 
} 
+0

我們不能追加使用jQuery一個輸入字段? – Crestamr

+0

你可以在這裏看到類似的部分:http://jsfiddle.net/xn2GP/1/你可以檢查我的粘貼在http://laravel.io/bin/RWXK – Crestamr

+0

檢查出 – johnRivs