1

Do not know which way is better to perform this operations.Having a two tables inquiry_master and inquiry_plan.Already define a relationship.

Migration files:

>> 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');
    });
}

Currently define a function to insert data only on inquiry_plantable. function which define on my controller.

Controller Code :

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();
        }
}

And finally save function on my model inquiryplan model.

Model code :

<?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;
    }
}}

Before saving a data on inquiry_plan table, one entry should go to the inquiry_master table.How can i do this ?

1 Answer 1

1

A couple things. First, Laravel already has exception handlers built into most if not all of it's functionality so providing you own exception is redundant. A good rule of thumb for this is - if you are expecting an error, this is not the case to use an exemption. So you can refactor this code significantly. As for the additional database entry, simply insert it before your inquiry_plan table logic:

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;
}}

Hopefully this helps!

As an aside, I would recommend splitting the create and update tasks into different controller methods...makes the code cleaner and helps for maintainability down the road.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.