2016-07-08 66 views
1

在我的用戶註冊頁面上,我有一個表單填寫他們的姓名,電子郵件等。我將在他們需要的東西列表中添加成就填寫。但是,我希望用戶能夠根據自己的需要放入儘可能多的成就。我稍後會在他們的個人資料頁面上顯示這些成就。如何使用HTML表單(Laravel)向數據庫添加多行

這是我的註冊表單,因爲它是,如果有幫助。

<form class="form-horizontal" role="form" method="POST" action="/register/make" enctype="multipart/form-data"> 
        {!! csrf_field() !!} 

        <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">First name</label> 

         <div class="col-md-6"> 
          <input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}"> 

          @if ($errors->has('first_name')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('first_name') }}</strong> 
           </span> 
          @endif 

         </div> 
        </div> 

        <div class="form-group{{ $errors->has('family_name') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Family Name</label> 

         <div class="col-md-6"> 
          <input type="text" class="form-control" name="family_name" value="{{ old('family_name') }}"> 

          @if ($errors->has('family_name')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('family_name') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">E-Mail Address</label> 

         <div class="col-md-6"> 
          <input type="email" class="form-control" name="email" value="{{ old('email') }}"> 

          @if ($errors->has('email')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('email') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Password</label> 

         <div class="col-md-6"> 
          <input type="password" class="form-control" name="password" value="{{ old('password') }}"> 

          @if ($errors->has('password')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('password') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Confirm Password</label> 

         <div class="col-md-6"> 
          <input type="password" class="form-control" name="password_confirmation" value="{{ old('password') }}"> 

          @if ($errors->has('password_confirmation')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('password_confirmation') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

         <div class="form-group{{ $errors->has('year') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Year Graduated</label> 

         <div class="col-md-6"> 
          <input type="text" class="form-control" name="year" value="{{ old('year') }}"> 

          @if ($errors->has('year')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('year') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('mark') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Final Mark</label> 

         <div class="col-md-6"> 
          <input type="text" class="form-control" name="mark" value="{{ old('mark') }}"> 

          @if ($errors->has('mark')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('mark') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

         <div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Description</label> 

         <div class="col-md-6"> 

          <textarea rows="10" class="form-control" name="description" value="{{ old('description') }}">@if ($errors->has('description')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('description') }}</strong> 
           </span>@endif</textarea> 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('picture') ? ' has-error' : '' }}"> 
         <label class="col-md-4 control-label">Upload a photo of yourself</label> 

         <div class="col-md-6"> 
          <input type="file" class="form-control" name="picture"> 

          @if ($errors->has('picture')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('picture') }}</strong> 
           </span> 
          @endif 
          <p>We recommend 300x300 pixels for best results.</p> 
         </div> 
        </div> 


        <div class="form-group"> 
         <div class="col-md-6 col-md-offset-4"> 
          <p>I agree to the <a href="/terms">terms and conditions</a></p> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="col-md-6 col-md-offset-4"> 
          <button name="register" type="submit" class="btn btn-primary"> 
           <i class="fa fa-btn fa-user"></i>Register 
          </button> 
         </div> 
        </div> 


        @if (count($errors)) 
         <ul> 
          @foreach($errors->all() as $error) 
           <li class="text-danger"> {{$error}} </li> 
          @endforeach 
         </ul> 
        @endif 


       </form> 

回答

0

只需使用括號中的字段名稱,像這樣:

<input type="text" name="achievements[]"/> 

然後使用JavaScript/jQuery的,您可以以編程方式爲用戶提供一個按鈕來添加一個新成果創造更多的領域。

您可以探索的另一個選擇是使用庫如select2。它具有tagging

在後端,PHP會將值轉換爲數組。然後Laravel給了你請求訪問這個數組,像這樣:

public function store(Request $request) 
{ 
    $achievements = $request->get('achievements'); 
} 

從那裏,你可以遍歷並保存每個成就就像你通常會。

相關問題