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 ?