kavi
kavi

Reputation: 149

how to view images that stored as array in laravel?

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>

Upvotes: 1

Views: 1996

Answers (3)

Mohamed Raafat
Mohamed Raafat

Reputation: 116

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.

Upvotes: 0

ramin ashrafimanesh
ramin ashrafimanesh

Reputation: 1062

Try :

@foreach($postqs['image'] as $imagePath)
    <img src="/image/{{ $imagePath }}" 
        style="width:150px; height:150px;"> 
@endforeach
</td>

Upvotes: 1

user320487
user320487

Reputation:

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

Upvotes: 0

Related Questions