gpt4 book ai didi

php - Laravel 仅在数据库表中存储数组的最后一个元素(foreach 循环)

转载 作者:行者123 更新时间:2023-11-29 09:44:25 24 4
gpt4 key购买 nike

我在数据库中上传了多张图片:

enter image description here在下一步中,我尝试根据 Blade 页面上动态创建的输入更改 alt 列的值:

enter image description here

但是在我提交并在 HTML 输入中输入值后,LARAVEL 应用程序仅在 alt 列中的两个图像(记录)中存储最后一个 HTML 输入的值(示例2)。

enter image description here

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);
}

最佳答案

这是因为您实际上在每个循环上加载所有 HomeGalery 模型。
$request->input('id') 将返回页面中所有 id 的数组,而不是特定于 $picture 的 id。

您最好只获取每个“alt”的 key ,然后使用 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) !!} 也在您的 Blade 文件中。

关于php - Laravel 仅在数据库表中存储数组的最后一个元素(foreach 循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55957481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com