首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 8的多态关系

Laravel 8的多态关系
EN

Stack Overflow用户
提问于 2022-02-05 10:30:33
回答 1查看 67关注 0票数 -2

Laravel医院管理系统表与处方、Presscription_medicine和药物表的关系。

的概念是presscription_medicine表的存储更多的药品通过参考一个处方id,而presscription_medicine行的药物在其中,然后在presscription_medicine表中的药品标识与药品表的关系。

如何做到这一关系,以及如何在处方表输出中获得如下药品名称

我对此已经厌倦了。

代码语言:javascript
复制
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表

代码语言:javascript
复制
<?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');
    }
}

医学表

代码语言:javascript
复制
<?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');
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-05 14:57:30

在“处方模型”类中,添加以下关系方法:

代码语言:javascript
复制
public function medicines()
{
    return $this->belongsToMany(Medicine::class,'prescription__medicines');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70997125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档