2016-05-16 39 views
0

我需要提交表單,並且此表單需要在Laravel的數據庫中的兩個不同的單獨表格中提交。如何在Laravel的模型中顯示多個表格

我有一個plan_notifications表,第二個是定價計劃表。我如何提交包含這兩個表的值的表單?任何幫助,將不勝感激?

AddPlanModel

class AddPlanModel extends Model { 

    protected $table = 'pricing_plan'; 

    protected $fillable = [ 
     'name', 
     'price', 
     'allowed_users', 
     'can_trail', 
     'trail_duration', 
     'detail1', 
     'detail2'‌​, 
     'staff_profiles', 
     'space_management', 
     'currency_unit', 
     'is_active', 
     'is_deleted' 
    ]; 

    public $timestamps = true; 
} 

NotificationModel

class NotificationModel extends Model { 

    protected $table = 'plan_notifications'; 

    public $timestamps = true; 

    protected $fillable = [ 
     'first_name', 
     'last_name', 
     'company_name', 
     'email', 
     'password', 
     'phone_number' 
    ]; 

    protected $hidden = ['password']; 

} 

查看

@extends('sadmin.main-template') 
@section('title', 'Super admin Dashboard') 

@section('title', 'Add Plan') 


@section('content') 
    <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12 right-sidebar admin-client add-client"> 
     @include('partials.errors') 
     <form action="{{url('add/plan')}}" class="toggle-disabled" method="post"> 
      {!! csrf_field() !!} 
     <h3 class="title">Add a plan</h3> 
     <div class="form-group"> 
      <label >You Can Create Your Plan Here</label> 
     </div> 
       <div class="row"> 

     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 left-client-title"> 
      <div class="form-group"> 
       <label class="form-label name"> 
        <span class="form-name">First Name</span> 
        <input type="text" class="form-control" id="name" name="first_name" placeholder="" data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$"> 
       </label> 
      </div> 
      <div class="form-group"> 
       <label class="form-label name"> 
        <span class="form-name">Last Name</span> 
        <input type="text" class="form-control" id="name" name="last_name" placeholder="" data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$"> 
       </label> 
      </div> 
      <div class="form-group"> 
       <label class="form-label name"> 
        <span class="form-name">Company Name</span> 
        <input type="text" class="form-control" id="name" name="company_name" placeholder="" data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$"> 
       </label> 
      </div> 
      <div class="form-group"> 
       <label class="form-label name"> 
        <span class="form-name">Email</span> 
        <input type="text" class="form-control" id="name" name="email" placeholder="" data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$"> 
       </label> 
      </div> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Password</span> 
       <input type="text" class="form-control" id="name" name="password" placeholder="" data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$"> 
      </label> 
     </div> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Phone Number</span> 
       <input type="text" class="form-control" id="price" name="phone_number" placeholder="" data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$"> 
      </label> 
      </div> 
     </div> 
     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 right-client-add"> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Allowed Users</span> 
       <input type="text" class="form-control" id="allowedUsers" name="allowed_users" placeholder=""data-validation="custom" data-validation-regexp="^([A-Za-z ]+)$" > 
      </label> 
     </div> 

     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Can Trail</span> 
       <input type="text" class="form-control" id="CanTrail" name="can_trail" placeholder="" data-validation="" > 
      </label> 
     </div> 
      </div> 
     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 left-client-title"> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Trail Duration</span> 
       <input type="text" class="form-control" id="trailDuration" name="trail_duration" placeholder="" data-validation=""> 
      </label> 
     </div> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Detail</span> 
       <input type="text" class="form-control" id="detail" name="detail1" placeholder="" data-validation=""> 
      </label> 
     </div> 
      </div> 
     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 right-client-add"> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Plan Detail</span> 
       <input type="text" class="form-control" id="detail" name="detail2" placeholder="" data-validation="custom" data-validation-regexp="^([0-9 +-]+)$"> 
      </label> 
     </div> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Staff Profile</span> 
       <input type="text" class="form-control" id="staffProfile" name="staff_profiles" placeholder="" data-validation="custom" data-validation-regexp="^([0-9 +-]+)$"> 
      </label> 
     </div> 
     </div> 
     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 left-client-title"> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Space Management</span> 
       <input type="text" class="form-control" id="spaceManagement" name="space_management" placeholder="" data-validation="custom" data-validation-regexp="^([0-9 +-]+)$"> 
      </label> 
     </div> 
      </div> 
        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 left-client-title"> 
     <div class="form-group"> 
      <label class="form-label name"> 
       <span class="form-name">Currency Unit</span> 
       <input type="text" class="form-control" id="currencyUnit" name="currency_unit" placeholder="" data-validation="custom" data-validation-regexp="^([0-9 +-]+)$"> 
      </label> 
     </div> 
         </div> 
      </div> 
      <p>&nbsp;</p> 
      <div class="row"> 
       <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center"> 
      <div align="center"> 
       <button type="submit" class="btn btn-success" style="padding-left:90px; padding-right:90px;"><strong>Send</strong></button> 
      </div> 
        </div> 
       </div> 
      </div> 
    </section> 


    </div> 

@endsection 
+1

請提供代碼! –

+0

這是我的AddPlanModel數據庫表名'class AddPlanModel extends Model { protected $ table ='pricing_plan'; protected $ fillable = ['name','price','allowed_users','can_trail','trail_duration','detail1','detail2','staff_profiles','space_management','currency_unit','is_active' , 'IS_DELETED']; public $ timestamps = true; }' –

+0

這是我的數據庫名稱通知模型'class NotificationModel extends Model { protected $ table ='plan_notifications'; public $ timestamps = true; protected $ fillable = ['first_name','last_name','company_name','email','password','phone_number']; protected $ hidden = ['password']; }' –

回答

0

模型只是爲了重新在數據庫中提供一張表。你應該爲每個你希望與之交互的表製作一個模型。

documentation

能言善辯的ORM包含Laravel提供了一個美麗的,簡單的ActiveRecord實現與數據庫的工作。 每個數據庫表都有一個對應的「模型」,用於與該表進行交互。

所以,爲表plan_notifications做樣板PlanNotification和表pricing_plans使另一個名爲PricingPlan。命名你的表的約定是複數,而你的模型應該是單數的。你當然可以重寫這個功能。

應該沒有什麼能夠阻止你從表單提交中提取數據,並創建兩個與數據庫分開的交互來在兩個單獨的表中插入兩個不同的記錄。

如果這兩個模型是相關的,您可以利用Eloquent's relationships

你會做你自己一個大忙通過該Eloquent documentation

編輯

讓在你做更多的Laravel嘗試閱讀文檔並完成教程:

https://laravel.com/docs/5.2/quickstart

https://laravel.com/docs/5.2/quickstart-intermediate

還有很大的視頻在這裏:

https://laracasts.com/series/laravel-5-from-scratch

至於你的問題,以及在這裏一些嚴重的猜測...

假設你已經設置了像這樣的路線:

Route::post('add/plan', '[email protected]'); 

你可以在你的PlanController做到這一點:

Use App\Notification; 
Use App\Plan; 

public function add(Request $request) 
{ 
    Notification::create([ 
     'first_name' => $request->get('first_name'), 
     'last_name' => $request->get('last_name'), 
     'company_name' => $request->get('company_name'), 
     'email' => $request->get('email'), 
     'password' => $request->get('password'), 
     'phone_number' => $request->get('phone_number'), 
    ]); 


    Plan::create([ 
     'allowed_users' => $request->get('allowed_users');, 
     'can_trail' => $request->get('can_trail'), 
     'trail_duration' => $request->get('trail_duration'), 
     'detail1' => $request->get('detail1'), 
     'detail2' => $request->get('detail2'), 
     'staff_profiles' => $request->get('staff_profiles'), 
     'space_management' => $request->get('space_management'), 
     'currency_unit' => $request->get('currency_unit'), 
     /* 
      missing from request: 
      name 
      price 
      is_active 
      is_deleted 
     */ 
    ]); 

    return 'done'; 
} 
+0

但我的表單視圖是單一的,我需要提交我的表格在兩個不同的表我知道我可以從雄辯關係做到這一點,但你可以在這裏繪製這些關係 –

+0

@shahzadhussain 1.沒有什麼能阻止你使用兩種不同的模型你的控制器插入兩個模型! 2.將你的表格視圖代碼添加到你的問題 – haakym

+0

模型之間沒有明顯關係 – haakym

相關問題