【问题标题】:How do I attach my table with others in Laravel?如何在 Laravel 中将我的桌子与其他人连接起来?
【发布时间】:2018-03-28 07:18:35
【问题描述】:

我是 Laravel 的新手。我想要这样:

当我存储正确存储在多个表中的数据时。存储后,它还将具有 disease_symptoms_medicines 表(外键)上所有三个表的 id。我不知道该怎么做。

在这段代码中没有错误。

这是我的迁移:

<?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateDiseasesTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('diseases', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->nullable();
                $table->string('slug')->nullable();
                $table->text('description')->nullable();
                $table->text('symptoms')->nullable();
                $table->timestamps();
            });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('diseases');
        }
    }

症状迁移

public function up()
{
    Schema::create('symptoms', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

药物迁移

public function up()
{
    Schema::create('diseases', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->nullable();
        $table->string('slug')->nullable();
        $table->text('description')->nullable();
        $table->text('symptoms')->nullable();
        $table->timestamps();
    });
}

为附加疾病、症状和医学的外键创建

疾病模型

    class Disease extends Model
    {
protected $table ='diseases';
public function symptoms(){

    return $this->belongsTo('App\Symptoms');
}
public function medicines(){

    return $this->belongsTo('App\Medicine');
}

症状模型

class Symptoms extends Model{
    protected $table ='symptoms';
    public function diseases()
    {
        return $this->hasMany('App\Disease');
    }
}

医学模型

    class Medicine extends Model{
    protected $table ='medicines';
    public function diseases(){

    $this->hasMany('App\Disease');
}
}

这是我的控制器

    public function store(Request $request)
{
    $disease=new Disease;
    $disease->name=$request->input('name');
    $disease->slug=$request->input('slug');
    $disease->description=$request->input('description');
    $disease->save();
    $symptom=new Symptoms;
    $symptom->name=$request->input('symtom');
    $symptom->save();
    $medicine=new Medicine;
    $medicine->name = $request->input('medic');
    $$medicine->save();

    return redirect()->back();
}

【问题讨论】:

  • 要记住的一件事;尝试给你的模型命名单数而不是复数。你已经为DiseaseMedicine 这样做了,但不是Symptoms。保持一致将使以后的事情更容易管理。
  • 好的,我下次再做。
  • 您的帖子中有各种拼写错误,这是一回事,但您的拼写错误正在泄漏到您的代码中。您有一个名为 symtom 的控件,但模型拼写正确 - 它有一个“p”。这种差异是另一个一致性问题,可能会在以后给您带来困难。

标签: php laravel eloquent foreign-keys


【解决方案1】:

您在迁移中犯了一个错误,当使用 eloquent 关系时,laravel 会寻找默认引用,除非您覆盖它。

例如

class Symptoms extends Model{
    protected $table ='symptoms';
    public function diseases()
    {
        return $this->hasMany('App\Disease');
    }

Laravel 会去疾病表中寻找症状_id

Eloquent relations

您需要放置正确的外键才能使关系起作用

改成这样:

public function up()
    {
        Schema::create('diseases', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();
            $table->string('slug')->nullable();
            $table->text('description')->nullable();
            //add the foreign key
            $table->integer('symptoms_id');
            $table->text('symptoms')->nullable();
            $table->timestamps();
        });
    }

要保存关系,请参阅https://laravel.com/docs/5.6/eloquent-relationships#the-save-method

【讨论】:

  • 我试过了,但什么也没发生,还是一样的问题。
  • @Mkamran:如果您需要更多帮助,您将需要提供帮助。你是问题的眼睛和耳朵,所以在回复时尽量做到充实和冗长。将 Stack Overflow 上的任何答案视为您自己研究的起点,而不是可以逐字输入的内容。您还可以向 Lupyana 提供哪些信息来帮助他们帮助您?
  • @Mkamran 您应该对其他模型执行相同的操作,即症状、医学和疾病模型。您需要适当的关系,否则它们将无法正常工作。你遇到了什么错误?你怎么称呼关系?也许这会让我深入了解您的问题
猜你喜欢
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 2023-04-09
相关资源
最近更新 更多