Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JMBDEV's avatar

Question on the"30 days to learn Laravel day 6 & 7 isue with aPHP function

HI, I have justa stupid issue when using the fn shortcut as in : Arr::first(static::all(),fn ($job)=>$job['id']=$id); $id is always marked as undentied variable and of course the function only return the first job , whatever id is given in the url... My IDE (PHPStorm...) marks $id as undentified, which is not a good sign , and I am using PHP8.3... I have : use Illuminate\Support\Arr; at the top of my class definition of course ; and even when wrinting this script in the web.php as described, it won't work either...

Don't see what can be wrong !!! Many tyhanks for getting me on the right path !

0 likes
5 replies
Tray2's avatar

To get help faster, always post the code, and not just a partial of the code, any code shared should be written between three backticks ```

That makes the code look like this

function myFunction()
{
	return myValue;
}
JMBDEV's avatar

OK, yes ,sorry & thanks at some point the function looks like below:

public static function find(int $id) : array      {
    return Arr::first(static::all(),fn ($job)=>$job['id']=$id);
}
JMBDEV's avatar

Hi Thanks for your answer, I guess I was not clear enough: This is the exact (...?) copy of a function pulled the laracast series ' 3 days to learn laravel', lessson 5 and 6 : I do undestand the scope limitations, but somehow, the script copied out of the lesson ( "Autolading, Namespaces, ..." )just does not work with me , below is the full Model script :

namespace App\Models;

use Illuminate\Support\Arr;

class Job { public static function all(): array { return [

        ['id' => 1,
            'title' => 'Director',
            'salary' => '90.000€'
        ],
        [
            'id' => 2,
            'title' => 'HR Manager',
            'salary' => '84.000€'
        ],
        [
            'id' => 3,
            'title' => 'Manager ',
            'salary' => '35.000€'
        ]
    ];
}


public static function find(int $id) : array      {
    return Arr::first(static::all(),fn ($job)=>$job['id']=$id);
}

}

Where $id is still undefined... which what I don't understand as I believe I am copy / pasting the origila lesson script

Snapey's avatar
Snapey
Best Answer
Level 122

You need to use comparison == not assignment =

public static function find(int $id) : array      {
    return Arr::first(static::all(),fn ($job)=>$job['id']==$id);
}
1 like
JMBDEV's avatar

Great ! Thanks ! Why would PHPStorm still flag '$id' as undefined ? Strange !

Please or to participate in this conversation.