如何在一个查询中存储多个数组 laravel
How to store multiple arrays in one query laravel
在视图文件中显示了比赛中每支球队的球员。
查看代码如下:
@foreach($match->homeTeam->players as $player)
<tr>
<th scope="row">{{$player->id}}</th>
<td class = "col-md-6" name = "fact[player]" value = "{{$player->id}}">{{$player->name}} {{$player->surname}}</td>
<td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" id="gridCheck"></td>
<td class = "col-md-2"><input id="minutes" type="integer" class="form-control" name="fact[minutes]"></td>
<td class = "col-md-2"><input id="goals" type="integer" class="form-control" name="fact[goals]"></td>
</tr>
@endforeach
所以我需要将它们存储在 Match_facts table.
如何在一个查询中插入所有内容?
匹配型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Team;
class Match extends Model
{
protected $fillable = ['round', 'league_id', 'home_team_id', 'away_team_id', 'match_date'];
public function leagues() {
return $this->belongsTo('App\League');
}
public function homeTeam() {
return $this->belongsTo('App\Team', 'home_team_id');
}
public function awayTeam() {
return $this->belongsTo('App\Team', 'away_team_id');
}
}
球员模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
public function team() {
return $this->belongsTo('App\Team');
}
}
在视图 addResult 中我显示了 Match(home_team and away_team)
以及每支球队的球员。
所以我想在 table (match_facts) 中为球队
的每个球员存储比赛事实
Schema::create('match_facts', function (Blueprint $table) {
$table->id();
$table->uuid('match_id');
$table->uuid('player_id');
$table->integer('minutes')->nullable();
$table->integer('goals')->nullable();
$table->timestamps();
});
成立的解决方案:
$home_team_players=$match->homeTeam->players;
$away_team_players=$match->awayTeam->players;
$match_players = $home_team_players->merge($away_team_players);
foreach ( $match_players as $k => $p ) {
$data[] = [
'match_id' => $match->id,
'player_id' => $p->id,
'minutes' => $request['minutes'][$k],
'goals' => $request['goals'][$k]
];
}
Match_fact::insert( $data );
return view('admin.matches.index')->with('success','ADDED SUCCESSFULLY .');
在视图文件中显示了比赛中每支球队的球员。
查看代码如下:
@foreach($match->homeTeam->players as $player)
<tr>
<th scope="row">{{$player->id}}</th>
<td class = "col-md-6" name = "fact[player]" value = "{{$player->id}}">{{$player->name}} {{$player->surname}}</td>
<td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" id="gridCheck"></td>
<td class = "col-md-2"><input id="minutes" type="integer" class="form-control" name="fact[minutes]"></td>
<td class = "col-md-2"><input id="goals" type="integer" class="form-control" name="fact[goals]"></td>
</tr>
@endforeach
所以我需要将它们存储在 Match_facts table.
如何在一个查询中插入所有内容?
匹配型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Team;
class Match extends Model
{
protected $fillable = ['round', 'league_id', 'home_team_id', 'away_team_id', 'match_date'];
public function leagues() {
return $this->belongsTo('App\League');
}
public function homeTeam() {
return $this->belongsTo('App\Team', 'home_team_id');
}
public function awayTeam() {
return $this->belongsTo('App\Team', 'away_team_id');
}
}
球员模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
public function team() {
return $this->belongsTo('App\Team');
}
}
在视图 addResult 中我显示了 Match(home_team and away_team) 以及每支球队的球员。 所以我想在 table (match_facts) 中为球队
的每个球员存储比赛事实Schema::create('match_facts', function (Blueprint $table) {
$table->id();
$table->uuid('match_id');
$table->uuid('player_id');
$table->integer('minutes')->nullable();
$table->integer('goals')->nullable();
$table->timestamps();
});
成立的解决方案:
$home_team_players=$match->homeTeam->players;
$away_team_players=$match->awayTeam->players;
$match_players = $home_team_players->merge($away_team_players);
foreach ( $match_players as $k => $p ) {
$data[] = [
'match_id' => $match->id,
'player_id' => $p->id,
'minutes' => $request['minutes'][$k],
'goals' => $request['goals'][$k]
];
}
Match_fact::insert( $data );
return view('admin.matches.index')->with('success','ADDED SUCCESSFULLY .');