Laravel Error Call to Member Function store() on Null

If you are uploading files using ajax in Laravel and at that time, face error fatal call in a member function store() on null, It means you are missing something in your ajax request or your controller file.

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

How to fix Laravel File Upload Call to Member Function store() on Null

Steps to fix Laravel file upload fatal error call to a member function store() on null:

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:

$(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;
use Illuminate\Support\File;

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']);
}

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:

chmod -R 755 storage

Step 5: Clear Configuration Cache

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

php artisan config:cache

Step 6: Change Route Method

Please change your ROUTE methods GET to POST; As follows:

Route::get('save',[TestController::class,'upload']);

Change to POST
Route::POST('save',[TestController::class,'upload']);

Step 7: 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 “Fatal Throwable Error: Call to a member function storage() or store 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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *