【问题标题】:Create eloquent relationships for Table with foreign keys to Pivot table - Laravel使用到数据透视表的外键为表创建雄辩的关系 - Laravel
【发布时间】:2019-08-30 06:09:07
【问题描述】:

我有一个数据透视表 (builder_project),用于处理“projects”和“builders”表之间的多对多关系。到目前为止一切正常。

但是,我希望用户能够将“notes”添加到数据透视表(builder_project),因为应该可以添加许多注释,所以我创建了一个名为“notes”的表格,它与数据透视表具有一对多关系

但是,我尝试为数据透视表创建模型;当我尝试访问数据透视模型中的方法时,出现以下错误:

SQLSTATE[42S22]:未找到列:1054 未知列“注释”。在 'where 子句' (SQL: select * from builder_project where exists (从notes 中选择 *,其中builder_project.project_projectID = notes.``))

这是我的枢轴模型的代码:

namespace Estimating;

use Illuminate\Database\Eloquent\Relations\Pivot;

class Builder_project extends Pivot
{


    protected $table = "builder_project";

    protected $primaryKey = 'project_projectID'; // I need to add the other primary key here but I undestand it's not possible on Laravel
    protected $fillable = [
        'project_projectID',
        'builder_builderID',
        'note',

    ];
    // One to many relationships
    /**
     * 
     */
    public function test()
    {
        return $this->hasMany('Estimating\Builder_project_note');
    }

}

这是 Builder_project_note 的模型:

<?php

namespace Estimating;

use Illuminate\Database\Eloquent\Model;

class Builder_project_note extends Model
{
    //Determines which database table to use, in this case 'projects' table
    protected $table = "notes";

    protected $primaryKey = 'noteID';
    protected $fillable = [
        'project_projectID',
        'builder_builderID',
        'note',

    ];

    // One to many relationships
    /**
     * Get the status that owns the project.
     */
    public function test()
    {
        return $this->belongsTo(
            'Estimating\Builder_project'); // I know that here I need an ID from Builder_project pivot table - but I have a composite key!;
    }


}

这就是我试图从我的控制器获取数据的方式,但我得到了错误:

public function editBuilderProject(Request $request, $builderID, $projectID)
    {
        $browserDetails = new Agent();
        $project = Project::find($projectID); 
        $builder = Builder::find($builderID);

        $builder_project_statuses = Builder_project_status::all();

        $builder_project_note = Builder_project::has('test')->get();

        dd($builder_project_note);



        return view('projects/edit-builder-project', compact('browserDetails', 'project','builder', 'builder_project_statuses', 'builder_project_note'));
        //return  $projectID."  ". $builderID;
    }

如果有人能指出正确的方向,我将不胜感激,如果我不够清楚,我深表歉意 - 这是我在这里的第一篇文章 :)

我上传了我的数据库模型的示例: Database Model example

【问题讨论】:

  • 请您也分享Builder_project_note 模型!
  • @RashedHasan - 感谢您的评论,Rashed - 我已将其添加到我原来的问题中。

标签: laravel laravel-5.8


【解决方案1】:

Laravel 不喜欢基于多列的主键。您需要添加自己的 builder_project.id 列(在您的数据透视表中),这将是您的 notes 表使用的字段。

【讨论】:

  • 感谢您的回答豪尔赫 - 我试图实施您的建议;但是,由于 'project_projectID''builder_builderID' 充当复合键,因此我无法添加另一个具有自动增量功能的主键,因为我收到错误消息。我还尝试将这两列设为非主键,但仍无法使新的 'builder_project.id' 列自动递增。
猜你喜欢
  • 2020-03-19
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多