Laravel 9中的更新方法仅更改1个值
我正在更新带有多照片的专辑(专辑表与图像表有许多关系),
但是当我使用更新方法时,它仅更改1个值,这是第一个图像,并且重置不更新,
但我无法弄清当我替换所有图像
编辑表单时,它都会更新所有图像
<h1 class="text-center">Edit {{$album->name}}'s Name </h1>
<div class="container">
<form method="post" action="{{route('albums.update',$album->id)}}" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="mb-3">
<label class="form-label">Album Name</label>
<input type="text" class="form-control @error('name') is invalid @enderror" name="name" value="{{$album->name}}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Album Category</label>
<input type="text" class="form-control @error('name') is invalid @enderror" name="category_id" value="{{$album->category_id}}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Image</label>
<input type="file" class="form-control @error('image') is-invalid @enderror" name="images[]" multiple>
@error('image')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
``
Update method in AlbumsController
public function update(Request $request, $id)
{
$album=Album::findOrFail($id);
$album->update([
'name'=>$request->name,
'category_id'=>$request->category_id,
]);
foreach($request->images as $image){
$albumImage=Time() . "_" . $image->getClientOriginalName();
$image->move('albums',$albumImage);
// $albumImage=Image::where('album_id', $id)->firstOrFail();
$albumImage2=Image::where('album_id', $id)->firstOrFail();
$albumImage2->update([
'album_id'=>$album->id,
'filename'=>$albumImage
]);
}
$albums=Album::all();
return view('admin.albums.all',compact('albums'));
}
I'm updating a album with multi photos (Albums table Has many relation with images Table)
but when i use the update method it change 1 value only which is the first image and the reset keep without updating
but i cannot figure the way that makes it update all images when i replace them
Edit Form
<h1 class="text-center">Edit {{$album->name}}'s Name </h1>
<div class="container">
<form method="post" action="{{route('albums.update',$album->id)}}" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="mb-3">
<label class="form-label">Album Name</label>
<input type="text" class="form-control @error('name') is invalid @enderror" name="name" value="{{$album->name}}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Album Category</label>
<input type="text" class="form-control @error('name') is invalid @enderror" name="category_id" value="{{$album->category_id}}">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Image</label>
<input type="file" class="form-control @error('image') is-invalid @enderror" name="images[]" multiple>
@error('image')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
``
Update method in AlbumsController
public function update(Request $request, $id)
{
$album=Album::findOrFail($id);
$album->update([
'name'=>$request->name,
'category_id'=>$request->category_id,
]);
foreach($request->images as $image){
$albumImage=Time() . "_" . $image->getClientOriginalName();
$image->move('albums',$albumImage);
// $albumImage=Image::where('album_id', $id)->firstOrFail();
$albumImage2=Image::where('album_id', $id)->firstOrFail();
$albumImage2->update([
'album_id'=>$album->id,
'filename'=>$albumImage
]);
}
$albums=Album::all();
return view('admin.albums.all',compact('albums'));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的行
每次都会带有相同的相关图像:第一个。
尝试用选定的图像替换所有专辑图像(删除全部,然后插入每个图像)。
Your line
will come back with the same related image every time: the first.
Try replacing all the album images with the ones selected (delete all, then insert each).