簡體   English   中英

如何在Laravel中的模型中顯示多個表格

[英]How can I display more than one table in models in Laravel

我需要提交一個表單,並且此表單需要在Laravel數據庫中的兩個不同的單獨表中提交。

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

添加計划模型

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

模型僅用於表示數據庫中的一個表。 您應該為要與之交互的每個表創建一個模型。

請參閱文檔

Laravel附帶的Eloquent ORM為使用您的數據庫提供了一個漂亮,簡單的ActiveRecord實現。 每個數據庫表都有一個對應的“模型”,用於與該表進行交互

因此,為表plan_notifications創建模型PlanNotification ,為表pricing_plans創建另一個名為PricingPlan 表的命名約定為復數形式,而模型應為單數形式。 您當然可以覆蓋此功能。

應該沒有什么阻止您從表單提交中提取數據並與數據庫創建兩個單獨的交互以將兩個不同的記錄插入兩個單獨的表中的。

如果兩個模型相關,則可以利用Eloquent的關系

通過閱讀Eloquent文檔,您將獲得極大的幫助

編輯

在做更多事情之前,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', 'PlanController@add');

您可以在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';
}

暫無
暫無

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

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