Laravel File Upload Ajax Fatalthrowableerror Call to Member Function Storage() on Null

[ad_1]

If you are uploading file using ajax in laravel and at that time, you may face laravel file upload ajax fatalthrowableerror call to a member function storage() on null.

Ezoic

So, in this tutorial, you will learn how to fix laravel file upload ajax fatalthrowableerror call to a member function storage() on null.

Laravel File Upload Ajax Fatalthrowableerror Call to Member Function Storage() on Null

Steps to fix laravel file upload ajax fatalthrowableerror call to a member function storage() on null:

  • Step 1: Verify AJAX Request
  • Step 2: Validate the RequestEzoic
  • Step 3: Configure Filesystem
  • Step 4: Check Permissions
  • Step 5: Clear Configuration Cache
  • Step 6: Test the File Upload

Step 1: Verify AJAX Request

Ensure that your AJAX request is properly configured and that it is sending data correctly to your Laravel backend. Make sure you have included the CSRF token if your application is using it for security.

Here’s an example of a simple laravel AJAX file upload request:

Ezoic

$(document).ready(function() {
    $('#file-upload').on('change', function() {
        let formData = new FormData();
        formData.append('file', this.files[0]);

        $.ajax({
            type: 'POST',
            url: '/upload',
            data: formData,
            contentType: false,
            processData: false,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function(response) {
                // Handle success
            },
            error: function(error) {
                // Handle error
            }
        });
    });
});

Step 2: Validate the Request

In your Laravel controller method that handles the file upload, make sure to validate the incoming request. Also, ensure you have included the necessary use statements at the top of your controller file:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

Here’s an example of how to validate and store an uploaded file in laravel:

public function uploadFile(Request $request)
{
    $request->validate([
        'file' => 'required|file|mimes:jpg,png,pdf|max:2048', // Adjust validation rules as needed
    ]);

    $file = $request->file('file');
    $path = $file->store('uploads'); // This assumes you have a 'uploads' disk configured in config/filesystems.php

    // You can store $path or any additional information in your database if needed.

    return response()->json(['message' => 'File uploaded successfully']);
}

Ezoic

Step 3: Configure Filesystem

Ensure that you have properly configured the filesystem in your Laravel application. Open config/filesystems.php and verify that you have a disk configuration that matches the storage location you are using. For example:

'disks' => [
    'uploads' => [
        'driver' => 'local',
        'root' => storage_path('app/uploads'), // Adjust the path as needed
    ],
],

Step 4: Check Permissions

Make sure that the storage directory and subdirectories have the appropriate permissions to allow file uploads. You can set the permissions in laravel using the following command:

Step 5: Clear Configuration Cache

Sometimes, Laravel’s configuration cache can cause issues. To clear cache in laravel, run the following command:

Step 6: Test the File Upload

Test the file upload functionality again by selecting a file in your form and triggering the AJAX request. You should now be able to upload files without encountering the “FatalThrowableError: Call to a member function storage() on null” error.

Conclusion

By following these steps and ensuring that your AJAX request, validation, filesystem configuration, and directory permissions are correctly set up, you should be able to fix this error and successfully handle file uploads in your Laravel application.

Recommended Tutorials

EzoicEzoic

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00