I'm working on a situation where I wanna create a new Quotation Followup to an existing Quotation. When I create a new Quotation Followup , everything but the Quotation id (foreign key) is saving.
My Followup migration is
public function up()
{
Schema::create('quotation_followup', function (Blueprint $table) {
$table->id();
$table->bigInteger('client_quotation_id')->unsigned()->index()->nullable();
$table->foreign('client_quotation_id')->references('id')->on('client_quotation')->onDelete('cascade');
$table->string('followup_method');
$table->mediumText('client_feedback');
$table->integer('done');
$table->date('date')->nullable();
$table->string('created_by');
$table->tinyinteger('holding_id')->default('1');
$table->timestamps();
});
}
My Quotation Followup Model:
class QuotationFollowup extends Model
{
use HasFactory;
protected $table = 'quotation_followup';
protected $fillable = [
'client_quotation_id',
'followup_method',
'client_feedback',
'done',
'date',
'created_by',
'holding_id'
];
public function quotation()
{
return $this->hasOne(ClientQuotation::class, 'client_quotation_id');
}
}
my Quotation Model:
class ClientQuotation extends Model
{
use HasFactory;
protected $table = 'client_quotation';
protected $fillable = [
'name',
'email',
'phone_number',
'contact_person',
'date_of_sending_inquiry',
'joinery_name',
'verification_date',
'notes',
'verified_receipt',
'created_by',
'holding_id'
];
public function followup()
{
return $this->belongsTo(QuotationFollowup::class, 'id', 'client_quotation_id');
}
}
my FollowupController is:
public function addfollowup(Request $request)
{
$quot = ClientQuotation::get();
$validatedData = $request->validate([
'followup_method' => 'required',
]);
$follow = new QuotationFollowup;
$quot = ClientQuotation::find(1);
$quot->followup()->associate($follow);
$follow->followup_method = json_encode($request->followup_method);
$follow->client_feedback = $request->input('client_feedback');
$follow->done = $request->done == true ? '1':'0';
$follow->date = $request->input('date');
$follow->created_by = auth()->user()->name;
$follow->save();
return redirect()->route('quot.index')->with('status','Quotation Followup Added Successfully');
}
My question is do I have the logic inverted in my mind? should the foreign key be on the quotation followup migration or the quotation migration? And in case I am on the right path, what should I change in the addfollowup function in the controller?
Thank you in advance.