The issue you're experiencing is likely due to the difference in PHP versions between your local environment (PHP 8.1.22) and the staging environment (PHP 8.1.18). One possible solution is to update your local environment to match the PHP version used in staging.
If updating your local PHP version is not an option, you can modify your code to handle the case where the contact with the given ID is not found. Currently, your show method assumes that the find method will always return a valid contact object. However, in some cases, it may return null, resulting in the "Attempt to read property 'first_name' on null" error.
To mitigate this issue, you can use the findOrFail method instead of find in your show method. This method will throw a ModelNotFoundException if the contact with the given ID is not found. You can then catch this exception and handle it appropriately, such as by displaying a custom error message or redirecting the user.
Here's an example of how you can modify your show method:
use Illuminate\Database\Eloquent\ModelNotFoundException;
public function show($id)
{
try {
$contact = Contact::findOrFail($id);
} catch (ModelNotFoundException $e) {
// Handle the case where the contact is not found
// For example, you can display a custom error message or redirect the user
return response('Contact not found', 404);
}
return view('crm.contacts.show', [
'contact' => $contact,
]);
}
By using findOrFail, you ensure that an exception is thrown if the contact is not found, allowing you to handle the error gracefully.
Remember to import the ModelNotFoundException class at the top of your controller file:
use Illuminate\Database\Eloquent\ModelNotFoundException;
This modification should help mitigate the issue in your staging environment. However, it's still recommended to keep your local environment in sync with the staging environment to avoid any potential compatibility issues.