首页 > 解决方案 > Laravel:我正在为我的数据库中的每个治疗师添加一个专业,这会显示出来。为什么?

问题描述

在此处输入图像描述

<?php


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

    Schema::create('t_specialties', function (Blueprint $table) {

        $table->integer('therapist_id');
        $table->integer('spec_id');
        $table->primary(['therapist_id','spec_id']);
        $table->timestamps();
    });
}

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

我的模型看起来像这样

class Therapist extends Model
{
    protected $fillable = ['image'] ;

    public function specialty()
    {
        return $this->belongsToMany(Specialty::class);
    }
}

我使用的方法类似于在博客中添加标签,但是每次我尝试在 cmd 中进行修改时,都会出现上图中的错误。我似乎无法理解有什么问题:(

标签: laravellaravel-5

解决方案


不介意回答。我已经找到了解决方案。我改变了模型中的一些东西!

这是我第一次使用的模型

class Therapist extends Model
{
 protected $fillable = ['image'] ;

 public function specialty()
 {
    return $this->belongsToMany(Specialty::class);
 }
}

我更新到这个

    public function specialty()
{
    return $this->belongsToMany('App\Specialty', 't_specialties', 'therapist_id', 'spec_id');
}

推荐阅读