Laravel Validation File Example
Teams applying “Laravel Validation File Example” in a production project can use the product team's article to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi guys, Today i will explained to the Laravel Validation file Example in your laravel project.Creating a feature to upload a file is a common task for any web developer; millions of photos, videos and documents daily updated to various websites.Laravel Validation file Example is so easy to use.so you can just follow my step by step and learn Laravel Validation file Example.
So let's start to the example and follow to the my all step.
Solution
$request->validate([
'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048'
]);
Example :
Step 1: Create Route
Last step to create a route in web.php file and use this code.
Route :routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UploadController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('upload/file', [UploadController::class, 'index']);
Route::post('upload/file/data', [UploadController::class, 'uploadFile'])->name('upload.file');
Step 2: Create a UploadController Controller
Next you can require to the UploadController so create a UploadController in just following command through.
Controller : app/Http/Controllers/UploadController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Validator;
class UploadController extends Controller
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index(){
return view('uploadFile');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function uploadFile(Request $request){
$request->validate([
'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048'
]);
$file = new File;
if($request->file()) {
$name = time().'_'.$request->file->getClientOriginalName();
$filePath = $request->file('file')->storeAs('uploads', $name, 'public');
$file->name = time().'_'.$request->file->getClientOriginalName();
$file->file = '/storage/' . $filePath;
$file->save();
return back()
->with('success','File has uploaded to the database.')
->with('file', $name);
}
}
}
Step 4: Create a UploadFile Blade File
Next you can require to the uploadFile.blade.php so create a uploadFile.blade.php in just following step.
View :resources/views/uploadFile.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<title>Laravel Validation File Example</title>
<style>
.container {
max-width: 450px;
}
dl, ol, ul {
margin: 0;
padding: 0;
list-style: none;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="card" style="width: 25rem;">
<h5 class="card-title text-center mt-3">Laravel Validation File Example</h5>
<div class="card-body">
<form action="{{route('upload.file')}}" method="post" enctype="multipart/form-data">
@csrf
@if ($msg = Session::get('success'))
<div class="alert alert-success">
<strong>{{ $msg }}</strong>
</div>
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="custom-file">
<input type="file" name="file" class="custom-file-input" id="chooseFile">
<label class="custom-file-label" for="chooseFile">Choose file</label>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-block mt-4">
Upload Files
</button>
</form>
</div>
</div>
</div>
</body>
</html>
So, finally we are done with our code we can get below output.
php artisan serve
Browser url run : http://localhost:8000/upload/file
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial