How to upload and store an image in database laravel 7

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering File Uploads in Laravel: Storing Images in the Database

It is completely normal to feel lost when tackling file uploads and database storage for the first time in a new framework like Laravel. The process involves coordinating front-end form handling, robust server-side validation, secure file system operations, and correct database relationships. Many tutorials jump straight to the code without explaining the underlying concepts, leading to those frustrating errors you experienced.

As a senior developer, I’m here to guide you through the exact, best-practice way to handle image uploads in Laravel 7 (and modern versions). We will move beyond simple file handling and implement a robust system that ensures your images are stored safely, linked correctly in the database, and easily accessible for display.

The Foundation: Setting Up the Form Correctly

The first crucial step is ensuring your HTML form is configured to handle binary file data. This is non-negotiable when uploading files.

Your provided HTML snippet already includes the most important part: enctype="multipart/form-data". This tells the browser to package the form data in a specific format that allows file uploads, which is essential for Laravel to process it correctly.

<form method="post" enctype="multipart/form-data" action="{{ route('category.store') }}">
    <!-- ... other fields ... -->
    <label for="imagename">Image</label>
    <input type="file" name="imagename" id="imagename"> <!-- The file input itself -->
    <!-- ... submit button ... -->
</form>

Without this attribute, Laravel simply cannot read the uploaded file data from the request.

Step-by-Step Implementation in the Controller

Now let’s focus on the controller logic where the magic happens. We need to retrieve the file, validate it, move it to a safe location on the server, and then save the resulting path to the database.

1. Handling the Request and Validation

Before attempting any file operations, always validate the incoming request. This protects your application from malicious uploads and handles errors gracefully. Use Laravel's built-in validation rules.

use Illuminate\Http\Request;
use App\Models\Category; // Assuming you have a Category model

public function store(Request $request)
{
    // 1. Validate the request first!
    $request->validate([
        'title' => 'required|string|max:255',
        'imagename' => 'required|image|mimes:jpeg,png,jpg|max:2048', // Ensure it's an actual image file
        'featured' => 'sometimes|boolean',
        'active' => 'sometimes|boolean',
    ]);

    // 2. Handle the File Upload
    if ($request->hasFile('imagename')) {
        $imageFile = $request->file('imagename');

        // Define the destination path (e.g., storage/app/public/categories/)
        $path = 'categories/';

        // Store the file on the disk
        $storePath = $imageFile->storeAs($path, time() . '_' . $imageFile->hashName(), 'public');
        
        // 3. Save to Database
        $category = new Category;
        $category->title = $request->input('title');
        $category->image_path = $storePath; // Store the path in the DB
        $category->featured = $request->boolean('featured');
        $category->active = $request->boolean('active');
        
        $category->save();

        session()->flash('success', "Category '{$category->title}' and image successfully created.");
        return redirect()->route('category.index');
    }

    // Handle case where no file was uploaded (though validation should catch this)
    return redirect()->back()->withErrors(['imagename' => 'Image file is required.']);
}

Best Practice: Utilizing the Laravel Storage Facade

Instead of manually managing file paths, which can lead to security issues and broken links if you move your application structure, always use Laravel’s built-in Storage facade. This abstracts away the complexities of interacting with local, S3, or other cloud storage drivers. As detailed in the official documentation, leveraging features like this is central to building scalable applications on https://laravelcompany.com.

By using $imageFile->storeAs(...), you let Laravel handle the secure writing of the file to the designated disk (in this case, the public disk), and it returns the relative path, which we then store in our database.

Conclusion: A Secure and Scalable Approach

Uploading and storing images is a multi-layered process that requires attention at every stage—from the client input to the server storage and final database link. By focusing on strong validation, using the storeAs method with the Storage facade, and ensuring your file paths are correctly saved in the database, you create a system that is not only functional but also secure and scalable.

Start with these fundamentals, embrace Laravel's powerful features, and you will be able to handle complex file management tasks with confidence!