本文介绍了在Laravel中创建并插入多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一个初学者,正在尝试学习所有我能学的东西.因此,如果我有错误,请纠正我.

First of all, i'm a beginner and trying to learn all i can. So if i have mistakes please correct me.

所以,我正在做一个laravel项目.我有两个模型.药物和相互作用.我正在努力的是,我想以一种形式添加两种药物和一种相互作用.而且,我想检查药物是否已经插入,以避免重复数据.

So, i am working on a laravel project. I've got two models; drugs and interactions. What i am struggling is, I want to add two drugs and one interaction in one form. But also, i want to check if the drug has inserted already to avoid duplicate data.

这是我的模特:

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中的存储功能

And this is simply the store function in my 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的存储功能.

And this is my InterationsController's store function.

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.我需要使其可变.但是,两种药物应与一种相互作用结合.因此,我无法一次成功地从表单中粘贴两个变量.

I can attach relationships via artisan tinker so i think relationship works. But i stuck when it comes to multiple input forms goes to different controllers. But using static id's. I need to make it variables. But two drugs should be attached to one interaction. So i couldn't succeed pasing two variables from form at the same time.

用愚蠢的话来说,我想要实现的是;

In dumb words, what i am trying to achieve is;

  • 从表单的第一个文本框中请求drug_name_one.检查db以获取该药物名称(如果存在);得到它的ID.如果没有,则创建一个并获取ID.
  • 从表单的第二个文本框中请求drug_name_two.与第一步相同.
  • 创建一个在表单的第三个文本框中键入的交互.
  • 附加它们.

PS:完成attach()工作后,如果两种药物具有共同的相互作用,我也找不到寻找两种药物的方法.如果您还可以提一些实现的技巧,我将不胜感激.

PS: after this attach() work done, i also couldn't find a way to search for two drugs if they have a common interaction. If you can also mention a few tips to achieve that i'll be grateful.

感谢所有帮助和进一步的阅读建议.谢谢大家!

All help and further reading advices appreciated. Thanks All !

这是create_interactions迁移.

This is the create_interactions migration.

Schema::create('interactions', function (Blueprint $table) {
     $table->BigIncrements('id');
     $table->string('name');
     $table->string('description');
     $table->string('category');
     $table->timestamps();
            });
        }

这是类别"字段的输入:

This is the input for 'category' field:

 <div class="form-group">
        {{Form::label('category', 'Kategori')}}
        {{Form::text('category', '', ['class' => 'form-control', 'placeholder' => 'Etkileşim Kategorisi'])}}
    </div> 

我无法按我的意愿制作表单的结构.这只是一种无需关联即可自行创建互动的形式.

I couldn't make the structure of a form as i desired btw. It's just the form for creating interactions by itself without relationship.

推荐答案

以下是解决方案.现在运行良好.

Here is the solution. It is working well now.

首先,我试图将2种药物分配给一种相互作用.因此,我已经导入了select2并使用此选择"表格列出了药物并从中选择了多种:

First of all i was trying to assign 2 drugs to one interaction. So i've imported select2 and used this "select" form to list the drugs and chose multiple from them:

 <div class="form-group">
                    {!! Form::label('İlaçları Seçin') !!}
                    {!! Form::select('drugs[]', $drugs, null, ['multiple' => 'multiple', 'class' => 'form-control drugs']) !!}
                </div>

在InteractionsController上:

On the InteractionsController:

$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表单上有以下脚本:

But i also wanted to show and edit those drugs assigned to the specific interaction. So on the edit.blade.php form there is this script:

<script type="text/javascript">
        $('.drugs').select2().val({!! json_encode($interaction->drugs()->allRelatedIds()) !!}).trigger('change');
    </script>

此脚本以与我在create.blade中使用的相同形式,将相关药物调用到选定的交互中.

This script, calls the related drugs to the selected interaction in the same form as i used in create.blade.

这篇关于在Laravel中创建并插入多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 08:55