Laravel 类别的DB结构有许多附件

Laravel 类别的DB结构有许多附件,laravel,eloquent,Laravel,Eloquent,所以,我一直在努力解决这个问题。在hasMany关系中,Laravel希望我在附件表中生成外键,这对于我的DB结构是错误的,我应该将它放在category表中 当我这样做的时候 /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $categories = Category::with('attachments')-&

所以,我一直在努力解决这个问题。在hasMany关系中,Laravel希望我在附件表中生成外键,这对于我的DB结构是错误的,我应该将它放在category表中

当我这样做的时候

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $categories = Category::with('attachments')->get();

    return $this->success(ApiResponseMessage::SUCCESS, $categories);
}
我得到以下错误:

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 
1054 Unknown column 'attachments.category_id' in 'where clause'
(SQL: select * from `attachments` where `attachments`.`category_id` = 1
and `attachments`.`category_id` is not null) in file 
F:\xampp\htdocs\svysh\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671
类别

class Category extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'slug'
    ];

    public function attachments()
    {
        return $this->hasMany('App\Attachment');
    }
}


public function up()
{
    Schema::create('attachments', function (Blueprint $table) {
        $table->id();
        $table->string('url');
        $table->string('width');
        $table->string('height');
        $table->timestamps();
    });
}
附件:

class Attachment extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'url', 'width', 'height', 'attachment_id'
    ];

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

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('slug');
        $table->integer('attachment_id')->nullable()->references('id')->on('attachment');
        $table->timestamps();
    });
}

根据您的要求,您的数据库设计是错误的,因为category
有许多附件/attachments
属于To
类别,因此attachments表应该包含一个
category\u id

您的
附件
表架构应如下所示:

public function up()
{
    Schema::create('attachments', function (Blueprint $table) {
        $table->id();
        $table->string('url');
        $table->string('width');
        $table->string('height');
        $table->foreignId('category_id')->constrained()->onUpdate('cascade');
        $table->timestamps();
    });
}
类别
表模式中不需要
附件id

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('slug');
        $table->timestamps();
    });
}
更新:根据OP的评论


附件不仅属于类别,还属于产品、用户和属性

在这种情况下,调整
附件
表架构以符合关系

public function up()
{
    Schema::create('attachments', function (Blueprint $table) {
        $table->id();
        $table->string('url');
        $table->string('width');
        $table->string('height');
        $table->integer('attachable_id');
        $table->string('attachable_type');
        $table->timestamps();
    });
}
并在模型中添加相应的方法定义

class Attachment extends Model
{

    /**
     * Get the owning attachable model.
     */
    public function attachable()
    {
        return $this->morphTo();
    }
}

class Category extends Model
{
  
    /**
     * Get all the Category's attachments.
     */
    public function attachments()
    {
        return $this->morphMany('App\Attachment', 'attachable');
    }
}

这是对的。这就是数据库的结构。附件不仅属于类别,还属于产品、用户和属性。如果我为附件模型中的每个关系添加一个id,最多只能填写1个,这违反了RDBMST的所有规则,您的问题中没有提供这些信息。在这种情况下,您可能希望调整数据库模式,使其符合@linktoahref的要求,如果将同一个附件用于所有这些模型,多态关系会起作用吗?例如,在产品、用户和属性中使用相同的图像是的,即使图像相同,图像(附件)的类型也会不同。@linktoahef是正确的。你的数据库应该按照他在下面提到的结构,而不是像你现在拥有的那样。附件不仅属于类别,它们还属于产品、用户和属性。如果我为附件模型中的每个关系添加一个id,最多只能填写1个,这违反了RDBMS的所有规则