[英]Laravel 8 Relationship
Laravel 医院管理系统表与处方、处方药和药品表的关系。
概念是presscription_medicine 表的存储更多的药通过引用一个处方id,并且每个presscription_medicine 行的有药,然后在presscription_medicine 表的medicine id 与药表的关系。
如何建立这种关系,以及如何在处方表 output 中获取药物名称,如下所示
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');
}
}
处方药表
<?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');
}
}
在处方 Model class 中添加此关系方法:
public function medicines()
{
return $this->belongsToMany(Medicine::class,'prescription__medicines');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.