DEV Community

Hòa Nguyễn Coder
Hòa Nguyễn Coder

Posted on

Show All Image from public folder using Laravel 5.8

Display all image from folder, we can using laravel
Create file FormController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FormController extends Controller
{
    public function index(){

        $images = \File::allFiles(public_path('images'));

        return View('pages.form')->with(array('images'=>$images));

    }
}
Enter fullscreen mode Exit fullscreen mode

We create file form.blade.php in folder view/pages/form.blade.php

@extends('layouts.app')

@section('content')

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <form action="">
                <h2>Show All Image from public folder using Laravel</h2>

                <ul>
                @foreach ($images as $image)
                     <li style="width:80px;display:inline-block;margin:5px 0px">
                         <input type="checkbox" name="images[]" value="{{$image->getFilename()}}"/>
                         <img src="{{ asset('images/' . $image->getFilename()) }}" width="50" height="50">
                     </li>
                @endforeach
                </ul>

            </form>
        </div>
    </div>
</div>


@endsection
Enter fullscreen mode Exit fullscreen mode

Config web.php in folder routes/web.php, following code

Route::prefix('form')->group(function () {
    Route::get('/','FormController@index')->name('form.index');
});
Enter fullscreen mode Exit fullscreen mode

Test:
php artisan serve

hoanguyenit.com

Post:Show All Image from public folder using Laravel 5.8
You can see more posts here: Share programming knowledge

Top comments (0)