I'm using Backpack for Laravel to provide the backend area of my laravel website.

I'm having the following tables in my database structure:

enter image description here

This is to add sets to a match, and add matches to a tournament.

These are my Models:

Tournament Model:

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

Match Model:

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

Set Model:

<?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

I can already set from, to, type, location and place. But now I would like the possibility to add a match and add sets to that match. This all on one page.

But I'm a bit stuck on how to do this. Can someone help me on my way?

推荐答案

我建议大家分多个步骤来做.创建锦标赛,转到锦标赛视图,添加比赛.转到匹配视图,添加集合.比你想做的要容易得多.但是,嘿,这是你的申请,你想一口气办完.

会有一些限制,比如你一次只能提交一场比赛,以避免将你的比赛组合混在一起.除非您想使用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, ...]),
    ]);
}

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

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

等等

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" />

等等

Since you want to make everything in one page. You can build a mini SPA just to add tournaments, matches & sets. The page won't change/reload.

Laravel相关问答推荐

Nuxt 3获取多部分表单数据上传不起作用

密码确认在Livewire中不匹配

Laravel 获取具有多对多关系的中间表数据

我在 laravel 中插入多个复选框值.我怎样才能做到这一点?

基于值laravel提交表单后如何返回特定页面

Laravel:如何通过 id 从集合中删除项目

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

在Laravel 5中通过ID和所有递归获取记录

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

如何比较laravel中的两个加密(bcrypt)密码

带有时间戳的 Laravel 5.1 eloquent::attach() 方法

Laravel 5.3 Electron邮件队列中不允许序列化关闭

如何重定向到laravel上的公共文件夹

Laravel 验证 pdf mime

Laravel Eloquent模型如何从关系表中获取数据

Laravel 数据库模式中的 MediumBlob

将参数传递给 Laravel 中的中间件

如何使用 Laravel 迁移在 Mysql 列中添加注释

Laravel or where

使用 Carbon 将小时转换为 PM 和 AM