How to Check If Request Has File in Laravel - Techvblogs

How to Check If Request Has File in Laravel

Learn how to check if request has file in Laravel for smooth file upload validation and error-free application flow.


Suresh Ramani - Author - Techvblogs
Suresh Ramani
 

2 days ago

TechvBlogs - Google News

In this article, I'll show you how to check a request has file in Laravel in this article. I'll let you know that the check request in Laravel has a file. I'll present a straightforward example and a solution. The idea behind the laravel hasFile example is clear.

Many times, we need to check request input file object is empty or not in laravel. If you need, we can provide you with the request object hasFile() method. We can check request has a file or not in the laravel controller. So let's understand in simple example code.

Example:

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'photo' => 'required',
        ]);
 
        if ($request->hasFile('photo')) {
            dd($request->photo);
        }
    }
}

Thank you for reading this article.

Read Also: How to get the raw SQL query from the Laravel Query Builder

Comments (0)

Comment


Note: All Input Fields are required.