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.
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 Request
- 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:
$(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']); }
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: 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.