0

I uploaded multiple images on the database:

enter image description here In the next step I'm trying to change the value of alt column based on dynamically created inputs located on the blade page:

enter image description here

But after I submit and entered values in HTML inputs, LARAVEL app stores only value (example2) from last HTML input in both images(records) within the alt column.

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

1 Answer 1

1

This is because you're actually loading all of the HomeGalery models on each loop.
$request->input('id') will return an array of all the ids in the page not the id that is specific to the $picture.

You would be better off just getting the key foreach 'alt' and then using 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);
}

This way you can get rid of {!! Form::hidden('id['.$picture->id.']', $picture->id) !!} in your blade file as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.