我有一个数据透视表(builder_project),该表用于'项目'和' builders '表之间的多对多关系。至此一切正常。
但是,我希望用户能够在数据透视表( builder_project )中添加'注释',因为应该可以添加许多注释,所以我创建了名为“ 注释”的表,该表与数据透视表具有一对多关系。
但是,我尝试为数据透视表创建模型;当我尝试在数据透视模型中访问该方法时,出现以下错误:
SQLSTATE [42S22]:找不到列:1054未知的列“注释”。在 'where子句'(SQL:从存在的
builder_project
中选择* (从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
答案 0 :(得分:0)
Laravel不喜欢基于多列的主键。您需要在数据透视表中添加自己的builder_project.id列,该列将成为便笺表使用的字段。