Laravel医院管理系统表与处方、Presscription_medicine和药物表的关系。
的概念是presscription_medicine表的存储更多的药品通过参考一个处方id,而presscription_medicine行的药物在其中,然后在presscription_medicine表中的药品标识与药品表的关系。
如何做到这一关系,以及如何在处方表输出中获得如下药品名称

我对此已经厌倦了。
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表
<?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');
}
}医学表
<?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');
}
}发布于 2022-02-05 14:57:30
在“处方模型”类中,添加以下关系方法:
public function medicines()
{
return $this->belongsToMany(Medicine::class,'prescription__medicines');
}https://stackoverflow.com/questions/70997125
复制相似问题