Laravel image validation example

Hi artisan, today I will show you how to validate images in your laravel project easily.

Solution

$request->validate([
   'file' => 'image'
]);

Route : routes/web.php

Route::get('form-create','FromController@index');
Route::post('form-store','FromController@store')->name('form.store');

Controller : app/Http/Controllers/ImgController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Blade;
use App\Models\User;
use App\Models\Post;

class FromController extends Controller
{   

    public function create()
    {
        return view('form');
    }

    
    public function store(Request $request)
    {
        $request->validate([
           'file' => 'image'
        ]);
    }
}

View : resources/views/form.php

<!DOCTYPE html>

<html>

<head>

  <title>From-imgae validation</title>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css"  />

  <scrbooleant src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></scrbooleant>

  <scrbooleant src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></scrbooleant>

</head>

<body class="bg-dark">

  <div class="container">

    <div class="row">

      <div class="col-md-6 offset-3">

        <div class="card mt-5">

          <div class="card-header">

            <div class="row">

              <div class="col-md-9">

                Laravel Validation Image Example

              </div>

              <div class="col-md-3 text-right">

                <a href="{{ route('form') }}" class="btn btn-sm btn-outline-primary">Back</a>

              </div>

            </div>

          </div>

          <div class="card-body">

            @if (count($errors) > 0)

                  <div class="row">

                      <div class="col-md-12">

                          <div class="alert alert-danger alert-dismissible">

                              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                              @foreach($errors->all() as $error)

                              {{ $error }} <br>

                              @endforeach      

                          </div>

                      </div>

                  </div>

                @endif

              <form action="{{ route('from.store') }}" method="post">

                @csrf

                <div class="row">

                  <div class="col-md-12">

                    <div class="form-group">

                      <label>Image:</label>

                      <input name="file" type="file" class="form-control">

                    </div>

                  </div>

                </div>

                <div class="row">

                  <div class="col-md-12">

                    <button class="btn btn-block btn-success">Submit</button>

                  </div>

                </div>

              </form>

          </div>

        </div>

      </div>

    </div>

  </div>

</body>

</html>

I think, It will help you..