0

enter image description hereI want to display all of my images that is displayed on my database in one ROW. The images are stores in an array in my database so it is in one ROW. Currently in my code the image is not showing and it is UNAVAILABLE

Here is my CONTROLLER

Storing Data

    $this->validate($request, [
        'promotion_image' => 'required'
    ]);

    if ($request->has('promotion_image'))
    {   
        //Handle File Upload

        $promotion = [];
        foreach ($request->file('promotion_image') as $key => $file)
        {
            // Get FileName
            $filenameWithExt = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $file->storeAs('public/promotion_images',$fileNameToStore);
            array_push($promotion, $fileNameToStore);
        }

        $fileNameToStore = serialize($promotion);
    }
    else
    {
        $fileNameToStore='noimage.jpg';
    }


if (count($promotion)) {
        $implodedPromotion = implode(' , ', $promotion);
        $promotionImage = new Promotion;
        $promotionImage->promotion_image = $implodedPromotion;
        $promotionImage->save();

        return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
    }

    return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');

Here is my VIEW

 @foreach($promotions as $promotion)
           <tr>  

            <th><img src="{{ asset('storage/promotion_images/' . $promotion->promotion_image) }}" style="width:50px;height:50px;"></th>
 @endforeach
11
  • Check your console. It will show the Image URL that couldn't load. According to that URL you need to modify the image path. Can you show you console Error please? Commented Nov 25, 2018 at 4:43
  • Please see the new edit. I added the problem .. there is no error just the image are not showing Commented Nov 25, 2018 at 4:46
  • @BanujanBalendrakumar I Edit the question above please see the image .. It has no error, my problem is that the images are not showing Commented Nov 25, 2018 at 4:46
  • The error only shows in console. you should check your path. Right-Click on image and click on Open Image in New Tab then show the image URL, And also show the directory tree. Commented Nov 25, 2018 at 4:48
  • @BanujanBalendrakumar I did that and it is said The Page cannot be found Commented Nov 25, 2018 at 5:01

2 Answers 2

0

All you have done for storing images seems okay, what you have to do is explode the image data store in the database. So, change your view code like this:

@foreach($promotions as $promotion)
       <tr>  

        <th>
            @foreach(explode(',' ,$promotion->promotion_image) as $image)
                <img src="{{ asset('storage/promotion_images/' . $image) }}" style="width:50px;height:50px;"><br/>
            @endforeach
        </th>
     </tr>
@endforeach

I think this will work for you. If you have any confusion regarding above code, feel free to ask.

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

5 Comments

SO CLOSE ... It stores 1 value .. the other image is not available why is that
@SummerWinter Can you please describe it in more detail so that i might help you
I have a dynamic form using jquery and all images are stored in one ROW.. your code showed me 1 image only I have 2 images in a row in my database.. so the other 1 image is not shown in my view.
@SummerWinter please add the screenshot of the database table
hey sir can you answer this question of mine please link
0
if (count($promotion)) {
        $implodedPromotion = implode(' , ', $promotion);
        $promotionImage = new Promotion;
        $promotionImage->promotion_image = $implodedPromotion;
        $promotionImage->save();

        return redirect('/admin/airlineplus/promotions',$promotion)->with('success', 'Image Inserted');
    }

    return redirect('/admin/airlineplus/promotions',$promotion)->with('error', 'Something went wrong.');

2 Comments

Can you explain your answer please?
what's that? it doesnt seems to help me

Your Answer

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