Laravel:我如何在多个表上插入数据

时间:2018-02-06 12:26:03

标签: laravel laravel-5.5

不知道哪种方式更适合执行此操作。有两个表inquiry_masterinquiry_plan。已经定义了一个关系。

迁移文件:

>> INQUIRY_MASTER

public function up()
{
   Schema::create('inquiry_master', function (Blueprint $table) {
        $table->increments('id');
        $table->dateTime('created_date_time')->nullable();
        $table->unsignedInteger('user_id')->nullable();
        $table->string('stitch_video_path')->nullable();
        $table->enum('completion_status',['Complete','Pending']);
        $table->foreign('user_id')->references('id')->on('users');
    });
}

>> INQUIRY_PLAN

public function up()
{
   Schema::create('inquiry_plan', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('inquiry_id');
        $table->string('title_vacation');
        $table->string('email');
        $table->string('phone_no', 20)->nullable();
        $table->dateTime('start_date')->nullable();
        $table->dateTime('end_date')->nullable();
        $table->foreign('inquiry_id')->references('id')->on('inquiry_master');
    });
}

目前定义一个仅在inquiry_plan表上插入数据的函数。 在我的控制器上定义的功能。

控制器代码:

public function addactivity(Request $request) {

        $validator = Validator::make($request->all(), Inquiryplan::planRules(), Inquiryplan::PlanMessages());
        if ($validator->fails()) {
            return back()->with('error', "Unable to send contact request.!!")->withInput()->withErrors($validator);
        }
        $plandetails = Inquiryplan::SaveOrUpdate($request);
        if($plandetails !== false) {
            return redirect()->route('plan')->with('success', trans('Plan details added successfully.!!'));
        } else {
            return back()->with('error', "Unable to save plan details.!!")->withInput();
        }
}

最后在我的模型inquiryplan模型上保存函数。

型号代码:

<?php

 namespace App;

 use Illuminate\Http\Request;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Input;

class Inquiryplan extends Model
{
protected $table = 'inquiry_plan';

protected $fillable = [
    'inquiry_id',
    'title_vacation',
    'email',
    'phone_no',
    'start_date',
    'end_date'
];

public $timestamps = false;

public static function SaveOrUpdate(Request $request) {
    try {
        $id = $request->get('id', false);
        $plandetails = false;
        DB::transaction(function () use ($request, &$plandetails, $id) {
            $plandetails = $id ? Inquiryplan::findOrFail($id) : new Inquiryplan();
            $plandetails->fill($request->all());
            try {
                $plandetails->save();
            } catch (\Exception $ex) {
                throw $ex;
            }
        });
        return $plandetails;
    } catch (\Exception $ex) {
        throw $ex;
    }
}}

inquiry_plan表上保存数据之前,一个条目应该转到inquiry_master表。我该怎么做?

1 个答案:

答案 0 :(得分:1)

一些事情。首先,Laravel已经在大多数(如果不是全部)功能中内置了异常处理程序,因此提供自己的异常是多余的。对此的一个好的经验法则是 - 如果您预计会出现错误,则不会使用豁免。因此,您可以显着重构此代码。至于附加数据库条目,只需将它插入到inquiry_plan表逻辑:

之前
public static function SaveOrUpdate(Request $request) {

    $id = $request->get('id', false);
    $plandetails = false;

    DB::transaction(function () use ($request, &$plandetails, $id) {

        $planDetails = $id ? Inquiryplan::findOrFail($id) : new Inquiryplan();

        //plan_master
        $inquiryMaster = ...

        //plan_details
        $planDetails->fill($request->all());
        $planDetails->save();

    });
    return $planDetails;
}}

希望这有帮助!

顺便说一句,我建议将创建和更新任务拆分为不同的控制器方法......使代码更清晰,并有助于维护性。