简体   繁体   English

如何使用 1 个表单使用 Laravel 插入多个表

[英]How to use 1 form to insert into multiple tables using Laravel

I have a form page for storing recording data of a specific artist.我有一个用于存储特定艺术家的录音数据的表单页面。 Most data belongs to the recording table but some data should be stored in related tables.大多数数据属于记录表,但有些数据应该存储在相关表中。 My main concern right now is the Genre table.我现在主要关心的是流派表。 In the genre table I have the Name column.在流派表中,我有名称列。 My idea of the ideal process after the form has been submitted is something like:我对表单提交后的理想流程的想法是:

  1. Check if the genre name already exists检查流派名称是否已存在
  2. If not: store the name and use its id as a fk如果不是:存储名称并将其 id 用作 fk
  3. If yes: fetch genre id and use it as a fk如果是:获取流派 ID 并将其用作 fk

What I am working on is the store method in the Recording controller.我正在研究的是 Recording 控制器中的 store 方法。 Currently I am working with $recording_date, $release_date and $lyrics from the recording table.目前我正在使用录音表中的 $recording_date、$release_date 和 $lyrics。

DISCLAIMER: I will not show you much of what I've tried because cannot show you all the ways I've tried and it's also useless cause the problem isn't that I don't understand why it isn't working.免责声明:我不会向您展示我尝试过的大部分内容,因为无法向您展示我尝试过的所有方法,而且这也是无用的,因为问题不在于我不明白为什么它不起作用。 I just can't find any way at all to do it properly with Eloquent that includes the same constraints and with a 1-to-many relation.我根本找不到任何方法来正确使用包含相同约束和一对多关系的 Eloquent。 Most people would suggest me to do it in separate forms.大多数人会建议我以不同的形式来做。

Also I'm aware of that I'm not handling the validation right now.我也知道我现在没有处理验证。 Anyway, any ideas or suggestions where I can find information of similar cases applicable on mine would help.无论如何,任何可以找到适用于我的类似案例信息的想法或建议都会有所帮助。

<!--Form snippet-->
<div class="form-group">
    <div class="input-group">
        <input type="text" name="genre" class="form-control" placeholder="Genre" autocomplete="off">
    </div>
</div>

The following was the latest piece of code I tried.以下是我尝试过的最新代码。 The obvious reason why it doesn't work is that it doesn't deal with the foreign key.它不起作用的明显原因是它不处理外键。

//Recording controller
public function store(Request $request)
    {
        $genre = new Genre();
        $genre->name = $request->input('genre');

        $recording = new Recording();
        $recording->genre_id = $genre->id;
        $recording->recording_date = $request->input('recording_date');
        $recording->release_date = $request->input('release_date');
        $recording->lyrics = $request->input('lyrics');
        $recording->save();
    }
//Recording table
public function up()
    public function up()
    {
        Schema::create('recordings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('genre_id')->nullable();
            $table->string('recording_date')->nullable();
            $table->string('release_date')->nullable();
            $table->text('lyrics')->nullable();
            $table->timestamps();
        });
    }
//Genres table
 public function up()
    {
        Schema::create('genres', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique()->nullable();
            $table->string('subgenre')->nullable();
            $table->timestamps();
        });
    }
//Recording model
class Recording extends Model
{
    protected $guarded = [];

    public function genre() {
        return $this->belongsTo(Genre::class);
    }
}
//Genre model
class Genre extends Model
{
    protected $guarded = [];

    public function recordings() {
        return $this->hasMany(Recording::class);
    }
}

In order to save onto relationship tables you have to do:为了保存到关系表,您必须执行以下操作:

$recording->genre()->create('your data array here')

you can read more about it here .你可以在这里阅读更多关于它的信息

Now if you want to check it out if that $recording has an genre you could first use exists() like:现在,如果您想检查 $recording 是否具有流派,您可以首先使用exists(),例如:

$recording->genre()->exists()

Another option would be to use the methods firstOrCreate, firstOrNew, you can read more about them here .另一种选择是使用方法 firstOrCreate、firstOrNew,您可以在此处阅读有关它们的更多信息。

Please try this请试试这个

....

$genre->save();

$genre->recordings()->create([
  "recording_date"=>$request->input('recording_date'),
  "release_date"=>$request->input('release_date'),
  "lyrics"=>$request->input('lyrics')
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM