Arrow functions were introduced in PHP 7.4
Arrow functions not working => Returns unexpected '=>' (T_DOUBLE_ARROW), expecting ')'
Hi guys,
I'm following along with Laravel from Scratch 8 however I've encountered a problem whenever trying to use the same code as Jeffery.
My code for searching works like this :
$query->when($filters['search'] ?? false, function ($query, $search) { $query ->where('title', 'like', '%' . $search . '%') ->orWhere('body', 'like', '%' . $search . '%'); });
Where as Jeffery moves on to this code:
$query->when($filters['search'] ?? false, fn($query, $search) => $query->where(fn($query) => $query->where('title', 'like', '%' . $search . '%') ->orWhere('body', 'like', '%' . $search . '%') ) );
The first way works fine for the initial search however when trying to use the => as shown next in the video I'm getting the Syntax error from the title. It wasn't a problem just for the search but moving on to episode 39, the search through categories I'm now following the video using => and it's just kicking up the same error. I've used "php artisan --version" to check and I'm on 8.64.0, also using "app()->version()" to double check along side the composer.json file so I'm definitely using the up to date version.
Any ideas where I'm going wrong? Thank you!
this...
fn($query, $search) => $query->
is arrow functions which was introduced in php 7.4 https://stitcher.io/blog/short-closures-in-php
This probably means you are running php before 7.4
Please or to participate in this conversation.