nie*_*lsv 17 php one-to-many laravel laravel-5 backpack-for-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');
}
}
Run Code Online (Sandbox Code Playgroud)
匹配模型:
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
设定型号:
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在后端创建锦标赛时有以下内容:
我已经可以设置from,to,type,location和place.但是现在我希望有可能添加一个匹配并为该匹配添加集合.这一切都在一页上.
但我有点坚持如何做到这一点.有人可以帮助我吗?
我会向所有人推荐多个步骤.创建锦标赛,进入锦标赛视图,添加比赛.转到匹配视图,添加集.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, ...]),
]);
}
Run Code Online (Sandbox Code Playgroud)
这意味着在您的表单中,您必须将您的集输入命名为
<input name="set1_self"/>
<input name="set1_opponent"/>
Run Code Online (Sandbox Code Playgroud)
等等.
现在,如果你想同时进行多个匹配,你必须找到一种方法来命名你的输入
<input name="match1_set1_self" />
<input name="match1_set2_self" />
Run Code Online (Sandbox Code Playgroud)
等等.
因为你想在一个页面中创建所有内容.您可以建立一个迷你SPA,只是为了添加比赛,比赛和比赛.页面不会更改/重新加载.
归档时间: |
|
查看次数: |
1309 次 |
最近记录: |