Skip to content
Advertisement

Multiple image retrieval with laravel

I have uploaded multiple images to database already. Now i am trying to display them in my view. while using foreach to loop through images i am getting error – Invalid argument supplied for foreach()

This is my blade

 @foreach($coverage as $cove)
            <tr>
                <td>{{$loop->iteration}}</td>
                <td>{{$cove->news1}}</td>
                <td>{{$cove->news2}}</td>
                <td>{{$cove->announcement1}}</td>
                <td>{{$cove->announcement2}}</td>
                <td>
                    @foreach($cove->gallery as $image)
                            <img src="{{'image/'.$image}}" style="height:200px;width:200px;"></td>
                    @endforeach
                <td>
                    <a href="{{route('edit',$cove->id)}}" class="btn btn-primary" style="display: inline-block">Edit</a>
                    <a onclick="alert('Do you want to delete')" href="{{route('delete',$cove->id)}}" class="btn btn-danger" style="display:inline-block">Delete</a>
                </td>
            </tr>
        @endforeach
        </tbody>

This is my store of controller

public function store(Request $request)
{

    $coverage = new coverage();
    $coverage->news1 = $request->news1;
    $coverage->news2 = $request->news2;
    $coverage->announcement1 = $request->announcement1;
    $coverage->announcement2 = $request->announcement2;
    $input = $request->all();
    $gallery = array();
    if ($files = $request->file('gallery')) {
        foreach ($files as $file) {
            $name = $file->getClientOriginalName();
            $file->move('image', $name);
            $gallery[] = $name;
        }
    }
    $coverage->gallery = json_encode($gallery);
    $coveragesave = $coverage->save();
    if ($coveragesave) {
        return redirect()->back()->with("success", "The record has been stored");
    } else {
        return redirect()->back()->with("error", "There is an error");
    }
}

This is how i have defined coverage table

 public function up()
{
    Schema::create('coverages', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('news1');
        $table->string('news2');
        $table->string('announcement1');
        $table->string('announcement2');
        $table->binary('gallery')->nullable();
        $table->timestamps();
    });
}

on dumping $coverage->gallery in store controller i receive this:

“[“70187225_683426155503359_4595847093867773952_n.jpg”,”70744555_683429382169703_8264311195482193920_n.jpg”]”

In database the same is saved as BLOB

Advertisement

Answer

Because $coverage->gallery is not an Array. Edit: I saw that your gallery column is type binary. I don’t know if it is possible to save a json_encoded String to binary column.

I would also reccommend you to use another table for gallery items with relationship to your coverage entries. Take a look at Eloquent Relationships

If you want to keep your current (and dirty) procedure:

If you store it with json_econde() you’ll have to decode it first if you want to loop over it.

Try changing gallery column to string and then do @foreach(json_decode($cove->gallery, true) as $image)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement