我在数据库上上传了多张图片:
在下一步中,我尝试根据位于刀片页面上的动态创建的输入来更改alt列的值:
但是在我提交并在HTML输入中输入值之后,LARAVEL应用程序仅将最后一次HTML输入中的值(example2)存储在alt列的两个图像(记录)中。
altPictures.blade.php
@extends('layout')
@section('content')
@include('nav')
<div class="container">
<h1 class="center-align">Insert alt Text for uploaded images</h1>
@if(count($errors) > 0)
<div class="materialert warning" id="close-dialog">
<i class="material-icons">warning</i>
<span>
@foreach($deletePost ->all() as $error)
<ul>
<li>{{$error}}</li>
</ul>
@endforeach
</span>
</div>
@endif
{!! Form::open(['action' => ['HomepageController@altPicturesPost', $language->url], 'method' => 'POST', 'files' => true]) !!}
@foreach($pictures as $picture)
<img class="center-block img-responsive" src="/uploads/homepage_galery/{{ $picture->filename }}"/>
<div class="form-group">
{{Form::label('naslov', 'Type alt text of image')}}
{{Form::text('alt['.$picture->id.']', '', ['id' => 'alt', 'class' => ($errors->has('naslov')) ? 'form-control is-invalid' : 'form-control', 'placeholder' => 'Type alt text of image '])}}
</div>
{!! Form::hidden('id['.$picture->id.']', $picture->id) !!}
@endforeach
{{Form::submit('Submit', ['class' => 'btn blog-button']) }}
{!! Form::close() !!}
</div><!-- /container -->
@endsection
HomeGaleryConroller
public function altPictures($url)
{
$languages = Language::where('url', '=', $url)->get();
$language = Language::where('url', '=', $url)->first();
if (count($language) == 0) {
abort(404);
}
$languagesLists = Language::all();
$languagesSelect = Language::pluck('title', 'id');
$categories = Category::with('firstsubcategories')->get();
$subcategories = SubCategory::All();
$secondsubcategories = SecondSubCategory::All();
$listings = CategoryListing::All();
$navposts = Post::all();
$navproducts = Product::all();
$subListings = SubcategoryListing::All();
$SecSubListings = SecSubcategoryListing::All();
$pictures = HomeGalery::all();
return view('homepageGalery/altPictures', ['languages' => $languages, 'language' => $language, 'languagesLists' => $languagesLists, 'categories' => $categories, 'listings' => $listings, 'subcategories' => $subcategories, 'secondsubcategories' => $secondsubcategories, 'adminpaneListings' => $adminpaneListings, 'navposts' => $navposts, 'navproducts' => $navproducts, 'adminpanelSublistings' => $adminpanelSublistings, 'adminpanelSecSublistings' => $adminpanelSecSublistings, 'subListings' => $subListings, 'SecSubListings' => $SecSubListings, 'languagesSelect' => $languagesSelect, 'pictures' => $pictures]);
}
public function altPicturesPost(Request $request, $url)
{
foreach ($request->input('alt') as $alt) {
$galeries = HomeGalery::wherein('id', $request->input('id'))->get();
foreach ($galeries as $galery) {
$galery->alt = $alt;
$galery->save();
}
}
return redirect()->route('adminpanel.homepage.galery', $url);
}
答案 0 :(得分:1)
这是因为您实际上是在每个循环上加载所有HomeGalery
模型。
$request->input('id')
将返回页面中所有ID的数组,而不是$picture
特定的ID。
最好先获取每个“ alt”的键,然后使用update()
:
public function altPicturesPost(Request $request, $url)
{
foreach ($request->input('alt') as $galleryId => $alt) {
HomeGalery::where('id', $galleryId)->update(compact('alt'));
}
return redirect()->route('adminpanel.homepage.galery', $url);
}
通过这种方式,您还可以摆脱刀片文件中的{!! Form::hidden('id['.$picture->id.']', $picture->id) !!}
。