首页 > 解决方案 > 在 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 中的存储功能

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> 

顺便说一句,我无法制作我想要的表单结构。它只是自己创建交互的形式,没有关系。

标签: phplaravelforms

解决方案


这是解决方案。现在运行良好。

首先,我试图将 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 中使用的相同形式将相关药物调用到选定的交互。


推荐阅读