gpt4 book ai didi

php - 形式上加一到多 - Backpack laravel

转载 作者:IT王子 更新时间:2023-10-29 00:13:22 26 4
gpt4 key购买 nike

我正在使用 Backpack for Laravel提供我的 laravel 网站的后端区域。

我的数据库结构中有以下:

enter image description here

这是为比赛添加套数,并为锦标赛添加比赛。

这些是我的模型:

锦标赛模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Tournament extends Model
{
use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $fillable = ['from', 'to', 'type', 'location', 'place'];

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function matches()
{
return $this->hasMany('App\Models\Match');
}
}

匹配模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Match extends Model
{
use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $table = 'matches';

protected $fillable = ['opponent'];

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function sets()
{
return $this->hasMany('App\Models\Set');
}
}

设置模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Set extends Model
{
use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $fillable = ['self', 'opponent', 'index'];

public function match()
{
return $this->belongsTo('App\Models\Match');
}
}

现在我想在后端创建锦标赛时拥有以下内容:

enter image description here

我已经可以设置 from、to、type、location 和 place。但是现在我希望可以添加匹配项并向该匹配项添加集合。这一切都在一页上。

但我对如何执行此操作有点困惑。有人可以帮我吗?

最佳答案

我会建议所有人分多个步骤进行。创建锦标赛,转到锦标赛 View ,添加比赛。转到匹配 View ,添加集。 Wayyyy 比你想做的更容易。但是,嘿,这是你的应用程序,你想一次性完成。

会有一些限制,例如您一次只能提交一场比赛,以避免混淆您的比赛。除非你想使用 javascript 并自动命名你的比赛和比赛。

在您的模型(匹配和设置)中,在 $fillable 数组中添加外键。我们可能需要它们。

因此,当您提交表单时,您就创建了锦标赛

public function save(Request $request)
{
$tournament = Tournament::create([
...
]);

$match = Match::create([
'tournament_id' => $tournament->id,
...
]);

$match->sets()->saveMany([
new Set(['self' => $request->set1_self, ...]), //foreign key auto inserted
new Set(['self' => $request->set2_self, ...]),
new Set(['self' => $request->set3_self, ...]),
]);
}

这意味着在您的表单中,您必须将您的集合输入命名为

<input name="set1_self"/>
<input name="set1_opponent"/>

等等。

现在如果你想同时进行多个匹配,你必须找到一种方法来命名你的输入

<input name="match1_set1_self" />
<input name="match1_set2_self" />

等等。

因为你想把所有的东西都放在一页上。您可以构建一个迷你 SPA 来添加锦标赛、比赛和组合。该页面不会更改/重新加载。

关于php - 形式上加一到多 - Backpack laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42515032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com