如何在Laravel中的模型中显示多个表

时间:2016-05-16 13:41:21

标签: php 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 :(得分:0)

模型仅用于表示数据库中的一个表。您应该为要与之交互的每个表创建一个模型。

请参阅documentation

  

Laravel附带的Eloquent ORM提供了一个漂亮,简单的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', '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';
}