Call to a member function store() on null - laravel 5.4
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Understanding and Resolving the Call to a Member Function store() on null Error in Laravel 5.4
Introduction: Troubleshooting and resolving errors is an essential part of any software development process. In this blog post, we'll explore the common issue "Call to a member function store() on null" that can occur while uploading files using Laravel 5.4. We will discuss possible reasons for this error and provide solutions based on your code example.
First, let us examine the given controller code:
```php
public function store(Request $request){
$file = $request->file('imgUpload1')->store('images');
return back();
}
```
The given issue has to do with the `$file` variable, as it currently points null before executing the `store()` method. Let's break down why this is happening and propose ways to resolve it.
Possible Reasons for the Error:
1. No file uploaded: This is possibly the most common issue, where users fail to select a file in the form or have no valid image to upload. In this case, checking if 'imgUpload1' exists and has a valid file before executing the store() method can prevent null pointer errors.
2. Incorrect form enctype: Your code sample includes `enctype="multipart/form-data"` which is the correct form attribute to use when submitting files. In case you have modified your original form, verify that it uses the same attribute to ensure proper handling of file uploads.
3. Invalid file path: The store() method appends the filename to a given storage directory by default if no specific path is provided. Ensure that Laravel has access to the specified directory or use an absolute path instead.
Solution 1 - Checking for File Existence:
To tackle this issue, we can add a simple check before storing the file.
```php
public function store(Request $request){
if ($file = $request->file('imgUpload1')) {
$file->store('images');
} else {
// Handle errors or redirect
}
return back();
}
```
Solution 2 - Handling File Upload Errors:
A more robust approach is to handle file upload errors and provide appropriate feedback to users.
```php
public function store(Request $request){
try {
if ($file = $request->file('imgUpload1')) {
$file->store('images');
} else {
// Handle errors or redirect
}
} catch (\Exception $e) {
// Log error and display appropriate message to users
}
return back();
}
```
Conclusion: Resolving the "Call to a member function store() on null" error in Laravel 5.4 involves examining the reasons behind it, such as missing file uploads or incorrect form attributes. By implementing checks for file existence and handling file upload errors, you can make your application more robust and user-friendly. As always, maintaining thorough documentation and testing will help ensure a smooth development process.