[Fix] Laravel: Return type of Illuminate\\Support\\Collection::offsetExists($key)

laravel

Error Message

During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Illuminate\\Support\\Collection::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/vpn/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1277\nStack trace:\n#0 

 

Cause

The issue is caused by the incompatible versions of PHP and Laravel. Laravel 7 and lower don’t support PHP 8.1 (which comes with Ubuntu 22.04).

laravel table
https://laravel.com/docs/8.x/releases#support-policy

Fix

There are two ways to address this issue. You can downgrade the PHP version to a version supported by your Laravel version, or you can upgrade Laravel to a version that is supported by the PHP version.

Change the PHP version to a version supported by your current version of Laravel.

Upgrading Laravel can be complicated, depending on your project and how big the leap is.  We’re going to cover the first method of fixing the issue.

Find out what version of Laravel your project is.

php artisan --version

In my case, the output is Laravel Framework 6.20.44.

By checking the table above, we see that we need PHP 7.2 – 8. We might also want to check the PHP version in the composer.json file from our project.

composer.json
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2",
        "david-griffiths/nova-dark-theme": "^1.0",
        "fideloper/proxy": "^4.0",
        "gregoriohc/laravel-nova-theme-responsive": "^0.6.2",
        "laravel/framework": "^6.0",
        "laravel/nova": "*",
        "laravel/tinker": "^1.0",
        "stephenlake/nova-fixed-bars": "^1.1",
        "titasgailius/search-relations": "^1.0"
    },
}

Check what version of PHP you’re running.

php -v
PHP 8.1 +ubuntu22.04.1+deb.sury.org+1 (cli) (built: Aug  1 2022 08:55:49) ( NTS )

Because we’re running a version of PHP that is not supported by our project, we need to downgrade it to PHP 7.2.

Install PHP 7.2 on Ubuntu 22.04

We need to set up the ppa:ondrej/php repository

sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt update

Install the PHP version you need and set it as default.

apt install php7.2

#you might want to install some common extentions as well
apt-get install php7.2 php7.2-cli libapache2-mod-php7.2 php7.2-common php7.2-curl php7.2-mbstring php7.2-xmlrpc php7.2-mysql php7.2-gd php7.2-xml php7.2-intl php7.2-ldap php7.2-imagick php7.2-json

Now, let’s set it as our main version in the command line.

update-alternatives --config php

Screen Shot 2022 09 05 at 4.19.55 PM

 

Check the PHP version again.

Screen Shot 2022 09 05 at 4.21.34 PM

Configure Apache to use the newly installed PHP 7.2 version

First, we need to disable the old PHP 8.1 from Apache

sudo a2dismod php8.1

Now, enable PHP 7.2

sudo a2enmod php7.2

Screen Shot 2022 09 05 at 4.25.30 PM

Restart Apache to apply the PHP version change.

service apache2 restart
0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
sooraj
sooraj
1 year ago

when i check the version on command line it shows 8.1 but when I run php_info it shows 7.2

george
george
2 months ago
Reply to  George A.

pls which terminal should i use in running this

DevMarcos17
DevMarcos17
13 days ago

This works for me. Thanks.

You May Also Like