形式一对多-背包laravel

尼尔斯

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

我的数据库结构中包含以下表格

在此处输入图片说明

这是向比赛添加组,并将比赛添加到比赛。

这些是我的模型

比赛型号:

<?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');
    }
}

现在,我在后端创建锦标赛时需要具备以下条件:

在此处输入图片说明

我已经可以从,到,类型,位置和地点进行设置。但是现在我希望可以添加一个匹配项,并为该匹配项添加集合。全部都在一页上。

但是我对如何做到这一点有些困惑。有人可以帮我吗?

EddyTheDove

I would recommend to all do in multiple steps. Create a tournament, go to tournament view, add matches. Go to a match view, add sets. Wayyyy easier than what you want to do. But hey, it's your application and you want to do in one go.

There are going to be some restrictions like you can only submit one match at a time, to avoid mixing your sets. Unless you want to use javascript and automatically name your matches and sets.

In your models (Match & Set) add foreign keys in the $fillable array. We might need them.

So when you submit the form, you create the tournament

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, ...]),
    ]);
}

This means in your form, you have to name your sets input like

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

And so on.

Now if you want to make multiple matches at the same time, you will have to find a way to name your inputs like

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

And so on.

由于您希望将所有内容都放在一个页面中。您可以建立一个迷你SPA来添加比赛,比赛和布景。页面不会更改/重新加载。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章