我有3个表:legalpursuit、guarantors、guarantors_legalpurchase。我的项目中有很多对很多的关系。我将legalpursuit_id保存在legalpurssuit/guarantors_id中,并将两者都保存在guarantors_legalphasce中。
我用的是laravel 8。如何显示每个帖子的标签?如何从tag_post表中选择数据?我使用它如下。但是它没有得到数据。
这是我的Legalpursuit模型:
public function guarantorses()
{
//return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
return $this->belongsToMany(Guarantors::class, 'guarantors_legalpursuit', 'legalpursuit_id', 'guarantors_id');
}
这是我的担保人模型:
public function legalpursuits()
{
//return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
return $this->belongsToMany(LegalPursuit::class,'guarantors_legalpursuit','guarantors_id', 'legalpursuit_id');
}
它是我的控制器:
public function edit(LegalPursuit $legalPursuit, $id)
{
$user = Auth::user();
$data = LegalPursuit::find($id);
$guarantors = $data->guarantorses()->orderBy('name')->get();
$lawyers = $data->lawyers()->orderBy('name')->get();
dd($guarantors);
$filesPursuit = LegalPursuit::find($id)->files;
$getLawyersLeft = DB::table('users')->where('user_type', '2')
->where('active', '1')
->where('deleted', '0')
->take('5')
->get();
return view('pages.legalpursuit.edit', compact('data', 'getLawyersLeft', 'filesPursuit', 'user'));
}
没有一个表名是不同的,它实际上完全遵循;
protected $table = 'guarantors';
该表的字段如下所示;
table name; guarantors
field: ID
field: name
pivot tablo名称和详细信息;
table name; guarantors_legalpursuit
id;
guarantors_id
legalpursuit_id
I can also do the adding process successfully as follows.
$guarantorArray = $request->input('GUARANTORS');
//$GuarantorsID = Guarantors::find($guarantorArray);
$data->guarantorses()->attach($guarantorArray);
但我不能提取关系数据。它不会给出错误。但是,尽管表中有数据,但数组感觉是空的。