【发布时间】:2022-01-06 15:53:48
【问题描述】:
您好,我正在尝试更新我的数据库中的数据,但我卡在这里我必须更新一个关系,但该关系也有一个数据透视表。
它应该搜索所有连接的“版主”,更新一个尚未连接的应该是,如果已经连接到“轨道”,那么应该会出现错误消息,但这不是现在的问题.它已经让我有机会附加其他版主,但不是获取错误消息的查询。
“Track”通过数据透视表与“Speaker”连接,在数据透视表上我有一列带有“is_moderator”。
这是我的 Track Controller 的更新功能:
public function update(Request $request, $id)
{
$track = Track::all()->find($id);
if ($request->file('track_data')) {
$hashedName = Hash::make($request->file('track_data')->getClientOriginalName()) . "." . $request->file('track_data')->getClientOriginalExtension();
$request->track_data->storeAs('public/tracks', $hashedName);
$track->data = $hashedName;
} elseif ($request->track_title) {
$track->title = $request->track_title;
} elseif ($request->track_description) {
$track->description = $request->track_description;
} elseif ($request->input('topics')) {
foreach($request->input('topics') as $topicIds){
$topicId = intval($topicIds);
$topicTrackId = $track->topics()->get();
for ($i = 0; $i < count($topicTrackId); $i++) {
if ($topicId == $topicTrackId[$i]->id) {
return redirect()->route('admin.trackEdit', $track->id)->withErrors(['msg' => 'Topic wurde bereits ausgewählt']);
}
}
}
$track->topics()->attach($request->input('topics'));
} elseif ($request->input('moderators')) {
$data = [];
foreach ($request->input('moderators') as $moderatorId) {
$data[$moderatorId] = ['is_moderator' => 1];
}
$track->speakers()->attach($data);
}
$track->update();
return redirect()->route('admin.trackShow');
}
这是我现在的 EditBlade:
@extends('layouts.app')
@section('content')
<form action="{{ route('admin.trackUpdate', $track->id) }}" method="post" enctype="multipart/form-data" id="track-form">
@csrf
{{ method_field('patch') }}
@include('errors.alert')
<label for="track_title">Track-Name</label>
<input type="text" name="track_title" id="track_title">
<label for="track_description">Track-Description</label>
<input type="text" name="track_description" id="track_description">
<label for="track_data">Track-Data</label>
<input type="file" name="track_data" id="track_data">
<select name="season_id" id="season_id">
<option selected value="">Keine Auswahl</option>
@foreach ($seasons as $season)
<option value="{{ $season->id }}">{{ $season->name }}</option>
@endforeach
</select>
<div class="container mt-5">
<label>Topics</label>
<select class="form-select" multiple="multiple" name="topics[]">
@foreach($topics as $topic)
<option value="{{$topic->id}}">{{$topic->name}}</option>
@endforeach
</select>
</div>
<div class="container mt-5">
<label>Moderator</label>
<select class="form-select" multiple="multiple" name="moderators[]">
@foreach($speakers as $speaker)
<option value="{{ $speaker->id }}">{{$speaker->getFullName()}}</option>
@endforeach
</select>
</div>
<div class="container mt-5">
<label>Speakers</label>
<select class="form-select" multiple="multiple" name="speakers[]">
@foreach($speakers as $speaker)
<option value="{{$speaker->id}}">{{$speaker->getFullName()}}</option>
@endforeach
</select>
</div>
<button type="submit">submit</button>
</form>
@endsection
赛道模型:
class Track extends Model
{
use HasFactory;
protected $table = 'tracks';
protected $primaryKey = 'id';
protected $fillable = [
'title',
'description',
'data',
'season_id',
];
public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
public function speakers(): BelongsToMany
{
return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id')->withPivot('is_moderator');
}
public function topics(): BelongsToMany
{
return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
}
}
还有扬声器模型:
class Track extends Model
{
use HasFactory;
protected $table = 'tracks';
protected $primaryKey = 'id';
protected $fillable = [
'title',
'description',
'data',
'season_id',
];
public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
public function speakers(): BelongsToMany
{
return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id')->withPivot('is_moderator');
}
public function topics(): BelongsToMany
{
return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
}
}
如果你能帮助我,我会很高兴我已经花了两个小时的头疼。 提前谢谢!
【问题讨论】:
-
不清楚您在这里要做什么。您的问题是更新“版主”?现在,您正在尝试将
[1 => ['is_moderator' => 1], 2 => ['is_moderator' => 1]]之类的数组与$track->speakers()同步,但这是行不通的。为什么会有所有elseif语句?他们将确保永远不会到达代码,除非其他值全部为空。
标签: php laravel pivot-table relationship laravel-8