php - Laravel Call to a member function getName() on null Error
Solution:
In the CLI/console, there's no request()
, nor is there a request()->route()
to get a name for.
You might consider having your helper check app()->runningInConsole()
and return a default there.
Answer
Solution:
I believe when you run the command there's no request route. Something like this would solve the issue where getName()
is not able to be returned on a non-object.
public static function get_lang()
{
$title_tag = __( request()->path() . '.title_tag' );
// Validate that request()->route() is not null
if(request()->route()) {
if( ( request()->route()->getName() !== 'index' ) ) {
abort( 404 );
}
}
}
Source