Search code examples
phplaravellaravel-8php-7.4php-8

syntax error, unexpected '=>' (T_DOUBLE_ARROW)


Context I just finished developing an internal business application using Laravel/Jetstream with Inertia which uses a Model Broadcaster with Ably integrated for desktop notifications. Everything was working perfectly locally so I pushed live this afternoon. I have everything migrated and what appears to be setup properly, with https working.

The server is a LEMP droplet on DO Link to the image details: https://marketplace.digitalocean.com/apps/lemp?ipAddress=143.198.148.3#getting-started

Issue: Everything I have read about the issue says it's likely a php version issue being earlier than 7.4, but this isn't the case here

The result of phpinfo():(I can't include the image because I just signed up today)

PHP Version 7.4.3

Image located at: https://i.ibb.co/C1qZyjZ/Screen-Shot-2021-07-21-at-3-56-20-PM.png

The result of var_dump(PHP_VERSION)

string(5) "7.4.3"

Inside of my Model Broadcaster is the origination of the error.

namespace App\Models;

use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Database\Eloquent\BroadcastsEvents;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Broadcasting\Channel;

class Ticket extends Model
{
    use BroadcastsEvents, HasFactory;

    /**
     * Get the channels that model events should broadcast on.
     *
     * @param  string  $event
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn($event)
    {
        return match($event) {
            //
            //This is the line
            //
            'created' => [new Channel('ticket')],
            default => []
        };
    }
}

Laravel Documentation showing the same format: https://laravel.com/docs/8.x/broadcasting#model-broadcasting

/**
 * Get the channels that model events should broadcast on.
 *
 * @param  string  $event
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn($event)
{
    return match($event) {
        'deleted' => [],
        default => [$this, $this->user],
    };
}

Solution

  • Match is php8 feature. You can update php7.4 to 8 or use switch

    switch($event)
        case 'created':
            return [new Channel('ticket')];
        default:
            return [];
    endswitch;