You can add macros to any class in Laravel that uses the Macroable
trait.
The main downside of macros is that your IDE doesn't understand them.
Calling a macro shows a warning that you're calling an undefined method.
Your IDE also won't know the return type of your macro.
You can make your IDE understand macros by adding a type definitions file to your project.
This is the same approach the Laravel IDE-helper package uses to add types for Laravel's magic.
Create a file at the root of your project called _ide_helper_custom.php
.
Make sure you commit this file to version control.
In this file, add definitions for all the macro's you've defined:
<?php
namespace Illuminate\Http
{
class Request
{
public function isRobot()
{
return true;
}
}
}
namespace Laravel\Dusk
{
class Browser
{
public function waitForThenClick($selector)
{
return $this;
}
public function assertSeeEmptyState()
{
return $this;
}
}
}
You can define all the classes and macros in this _ide_helper_custom.php
file.
As long as the method name, arguments, and return type match the actual macro definition, your IDE will understand and correctly autocomplete your macros.
Level up your Laravel deployments
If you aren't happy with your current deployment strategy for your Laravel apps, check out my premium deployment script.
Deploy with GitHub Actions
Deploy with GitLab CI/CD