Laravel Hospital Management System Table Relation With Prescription, Presscription_medicine and Medicine Table.
the concept is presscription_medicine table's store more medicine by reference one prescription id, and each presscription_medicine row's has medicine in , then in presscription_medicine table's medicine id relation with medicine table.
how to do this relations, and how to get medicine name in prescription table output as like this
I have already tired for this.
Prescription Migration table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePrescriptionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('prescriptions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('prescription_code', 45);
$table->string('prescription_p_id');
$table->unsignedBigInteger('prescription_doc_id');
$table->string('prescription_history', 220);
$table->string('prescription_note', 220);
$table->string('prescription_date',45);
$table->timestamps();
// $table->foreign('prescription_p_id');
//->references('in_p_s', 'out_p_id')->on('in_patients', 'out_patients')->onDelete('cascade');
$table->foreign('prescription_doc_id')->references('id')->on('doctors');
//->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('prescriptions');
}
}
Prescription_medicine table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePrescriptionMedicinesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('prescription__medicines', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('prescription_id');
$table->unsignedBigInteger('prescription_medicine_id');
$table->string('prescription_med_dosage', 45);
$table->string('prescription_med_frequency', 45);
$table->string('prescription_med_days', 45);
$table->string('prescription_med_ins', 45);
$table->timestamps();
$table->foreign('prescription_id')->references('id')->on('prescriptions')->onDelete('cascade');
$table->foreign('prescription_medicine_id')->references('id')->on('medicines')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('prescription__medicines');
}
}
Medicine Table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMedicinesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('medicines', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('price');
$table->integer('mg');
$table->string('group');
$table->string('company');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('medicines');
}
}
in Prescription Model class add this relation method:
public function medicines()
{
return $this->belongsToMany(Medicine::class,'prescription__medicines');
}