Laravel 8 关系

Laravel 8 Relationship

Laravel 医院管理系统 Table 与处方、Presscription_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.

如何做这个关系,以及如何得到方子中的药名table这样输出

我已经厌倦了这个。

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');
    }
}

医学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');
    }
}

在处方模型class中添加此关系方法:

public function medicines()
{
    return $this->belongsToMany(Medicine::class,'prescription__medicines');
}