[英]Create and Insert Many-to-Many Relationships in Laravel
首先,我是一个初学者,我想尽我所能地学习。 所以如果我有错误,请纠正我。
所以,我正在做一个 Laravel 项目。 我有两个模型; 药物和相互作用。 我正在挣扎的是,我想以一种形式添加两种药物和一种相互作用。 而且,我想检查药物是否已经插入以避免重复数据。
这是我的模型:
class Drug extends Model
{
//Table Name
protected $table = 'drugs';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamps = true;
//relationship
public function interactions()
{
return $this->belongsToMany('App\Interaction', 'drug_interaction', 'interaction_id', 'drug_id');
}
}
class Interaction extends Model
{
//Table Name
protected $table = 'interactions';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamps = true;
//Relationship
public function drugs()
{
return $this->belongsToMany('App\Drug', 'drug_interaction', 'drug_id', 'interaction_id');
}
}
这只是我 DrugsController 中的 store 函数
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required',
'info'=> 'nullable'
]);
//create drug
$drug = new Drug;
$drug->name = $request->input('name');
$drug->info = $request->input('info');
$drug->save();
return redirect('/drugs')->with('success', 'İlaç Eklendi');
}
这是我的 InterationsController 的存储功能。
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required',
'description'=> 'required',
'category'=> 'nullable'
]);
//create interaction
$interaction = new Interaction;
$interaction->name = $request->input('name');
$interaction->description = $request->input('description');
$interaction->category = $request->input('category');
$interaction->save();
我可以通过工匠修补程序附加关系,所以我认为关系有效。 但是当涉及到多个输入表单到不同的控制器时,我被卡住了。 但是使用静态ID。 我需要使它成为变量。 但是两种药物应该与一种相互作用相关联。 所以我无法同时从表单中成功传递两个变量。
用愚蠢的话来说,我想要实现的是;
PS:在此 attach() 工作完成后,如果它们有共同的相互作用,我也找不到搜索两种药物的方法。 如果您还可以提及一些实现这一目标的技巧,我将不胜感激。
感谢所有帮助和进一步阅读建议。 谢谢大家!
编辑:
这是 create_interactions 迁移。
Schema::create('interactions', function (Blueprint $table) {
$table->BigIncrements('id');
$table->string('name');
$table->string('description');
$table->string('category');
$table->timestamps();
});
}
这是“类别”字段的输入:
<div class="form-group">
{{Form::label('category', 'Kategori')}}
{{Form::text('category', '', ['class' => 'form-control', 'placeholder' => 'Etkileşim Kategorisi'])}}
</div>
顺便说一句,我无法像我想要的那样制作表格的结构。 它只是一种没有关系的自行创建交互的形式。
这是解决方案。 现在运行良好。
首先,我试图将 2 种药物分配给一个相互作用。 所以我导入了 select2 并使用这个“选择”表单来列出药物并从中选择多个:
<div class="form-group">
{!! Form::label('İlaçları Seçin') !!}
{!! Form::select('drugs[]', $drugs, null, ['multiple' => 'multiple', 'class' => 'form-control drugs']) !!}
</div>
在交互控制器上:
$interaction = new Interaction;
$interaction->name = $request->input('name');
$interaction->description = $request->input('description');
$interaction->category = $request->input('category');
$interaction->save();
$interaction->drugs()->sync($request->drugs, false);
但我也想显示和编辑分配给特定相互作用的药物。 所以在 edit.blade.php 表单上有这个脚本:
<script type="text/javascript">
$('.drugs').select2().val({!! json_encode($interaction->drugs()->allRelatedIds()) !!}).trigger('change');
</script>
该脚本以我在 create.blade 中使用的相同形式将相关药物调用到选定的交互中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.