1

user upload multiple image and it stored in array form. how to display those images in laravel blade file?
when i tried i got this error.

htmlspecialchars() expects parameter 1 to be string, array given (View: C:\Users\user\laravel2\newfyp\resources\views\postqs\show.blade.php)

show blade :

   <img src="/image/{{ $postqs['image'] }}" 
    style="width:150px; height:150px;"> </td>

model:

  protected $casts = [
    'image'=>'array', ];

 protected $fillable = ['image'];

create blade

                <div class="form-group">
                    <label class="control-label col-sm-2" >image test:
             </label>
                    <form action="upload" id="upload" enctype="multipart/form-data">
                    <input type="file" name="image[]" id="image" multiple><br />

                </form>
                <div id="message"></div>
3
  • Possible duplicate of Laravel 5.3 multiple file uploads Commented Jan 19, 2018 at 8:38
  • could you show the dummped array code ? I mean dump the array and show the results here Commented Jan 19, 2018 at 8:39
  • $postqs['image'] is an array not a string Commented Jan 19, 2018 at 8:44

3 Answers 3

1

Try :

@foreach($postqs['image'] as $imagePath)
    <img src="/image/{{ $imagePath }}" 
        style="width:150px; height:150px;"> 
@endforeach
</td>
Sign up to request clarification or add additional context in comments.

Comments

0
protected $fillable = [''image'];

Is that a typo with the double quote?

If the images are in an array simply loop over the array and create an img for each.

Have you tried something like the following in your view file:

@foreach ($postqs['image'] as $image)
    <img src="asset({{ $image }})" />
@endforeach

1 Comment

How are you looping the array? If the path is correct for each image within the array it should resolve no problem. I use the asset helper to link to public resources also like: <img src={{ asset('/images/someimage.png') }} />
0

First of all you should correct this line:

protected $fillable = [''image']; // ['image'] and not [''image']

htmlspecialchars(): I always get this error when the variable is null.

A simple solution would be wrap your html with:

@if($yourVar)
@endif

You can also dump your variable to show how the structure is.

Good luck.

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.