Malcolm Turntbull
Malcolm Turntbull
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
ok, that actually makes a lot of sense!
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
although for the sake of explaining "where" you can use this sort of injection, I think I probably know enough to explain it
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
now to try and find where the infoList gets that record in the first place...
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
so in my instance, the first one worked because it was on an $infoList, which has its record already defined
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
I've done some digging, and so far I've worked out that essentially, the "context" is that you're evaluating the closure on an object that has $record set.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
Is there any documentation on that "context"? I would love to be able to add it to my article, and maybe help the next idiot like me who doesn't know when to quit 😛
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
Even if I call Filament's internal evaluate() method, it doesn't just resolve a model from wherever you like
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
There has to be some sort of context around which Filament's DI does and does not work - for an example, there's this:
class EnvironmentService
{
use Filament\Support\Concerns\EvaluatesClosures;

public function getEnvironment()
{
return $this->evaluate(fn (Environment $environment) => $environment);
}
}

Action::make("action")
->action(function (Environment $environment) {
\Debugbar::alert($environment); //This works, shows the current Environment
$service = app(EnvironmentService::class);
\Debugbar::alert($service->getEnvironment()); //Blank Environment
});
class EnvironmentService
{
use Filament\Support\Concerns\EvaluatesClosures;

public function getEnvironment()
{
return $this->evaluate(fn (Environment $environment) => $environment);
}
}

Action::make("action")
->action(function (Environment $environment) {
\Debugbar::alert($environment); //This works, shows the current Environment
$service = app(EnvironmentService::class);
\Debugbar::alert($service->getEnvironment()); //Blank Environment
});
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
There are countless thousands of examples of how DI works, exactly like the base Laravel docs show you. There's almost nothing on why and when it DOESN'T work.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
https://docs.google.com/document/d/19YhjppE5qSw4v1xqaDQFiuHmEjiPCpJyfFOHHY21YTE/edit?usp=sharing this is the start of my documentation, I'll write it in here for now, and eventually publish on Medium.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
This whole thing has been such a handbrake on my project, I'm writing some documentation on how it all actually works - only baby steps so far, as I clearly don't actually understand wtf I am doing, but if I am running into these sorts of misunderstandings, then it's guaranteed other people are as well, and I've spent... WEEKS looking at documentation on this.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
I have foolishly believed that if there is DI happening, that it must be the SC doing it. And that if the SC was able to resolve a specific instance of a Model, then it was Route Model Binding doing it.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
I have also clearly misunderstood the Laravel documentation - when it says "The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection", it does not mean that it is THE tool for managing dependency injection.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
So when Filament is injecting the correct model in that instance, it's doing so via its own methodology, and has nothing to do with the container, OR route model bindings (which I don't think actually work at all in Filament, because we're not using Controllers?)
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
OK, so I think I may have stumbled onto an underlying truth that I didn't really understand: Route Bindings and Container Bindings are two completely separate things.
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
I've tried cleaning up the code to make it more obvious as to what goes where, and I've also commented all the various attempts that do and do not work:
class ProjectResource extends Resource
{
protected static ?string $model = Project::class;
protected static function repositorySection(): ?Section
{
return Section::make('repository-setup')
->heading("Setup Actions")
->schema(
function (Project $project) {
\Debugbar::alert($project); // WORKS
\Debugbar::alert(app(Project::class)); // DOES NOT WORK
$playbook = app(Playbook::class)::make();
\Debugbar::alert($playbook->project); // CONSTRUCTOR INJECTION, DOES NOT WORK AS EITHER $project OR $record
//---
}
);
}
}

class Playbook
{
public function __construct(
public Project $project,

) {}
public static function make($class = null): Playbook
{
$playbook = app($class ?? static::class);
\Debugbar::alert(app(Project::class)); // DOES NOT WORK
$playbook->configureDto();
return $playbook;
}

protected function configureDto()
{
\Debugbar::alert(request()->getPathInfo()); //SHOWS PATH WITH /projects/ EVEN THIS DEEP
$project = app(Project::class);
\Debugbar::alert($project); //DOES NOT WORK
\Debugbar::alert($this->project); //TESTED ALREADY, BUT DOES NOT WORK
}
}
class ProjectResource extends Resource
{
protected static ?string $model = Project::class;
protected static function repositorySection(): ?Section
{
return Section::make('repository-setup')
->heading("Setup Actions")
->schema(
function (Project $project) {
\Debugbar::alert($project); // WORKS
\Debugbar::alert(app(Project::class)); // DOES NOT WORK
$playbook = app(Playbook::class)::make();
\Debugbar::alert($playbook->project); // CONSTRUCTOR INJECTION, DOES NOT WORK AS EITHER $project OR $record
//---
}
);
}
}

class Playbook
{
public function __construct(
public Project $project,

) {}
public static function make($class = null): Playbook
{
$playbook = app($class ?? static::class);
\Debugbar::alert(app(Project::class)); // DOES NOT WORK
$playbook->configureDto();
return $playbook;
}

protected function configureDto()
{
\Debugbar::alert(request()->getPathInfo()); //SHOWS PATH WITH /projects/ EVEN THIS DEEP
$project = app(Project::class);
\Debugbar::alert($project); //DOES NOT WORK
\Debugbar::alert($this->project); //TESTED ALREADY, BUT DOES NOT WORK
}
}
Is there a limit on the number of times a record can be injected? Or do nested injections not work? I'm losing my mind here, what am I doing wrong?
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
OK, I just tried this:
function (Project $project) {
\Debugbar::alert($project);
\Debugbar::alert(app(Project::class));
$playbook = Playbook::make($project->template);
return $playbook->getPlays()->map(function (Play $play) {
//---
});
}
function (Project $project) {
\Debugbar::alert($project);
\Debugbar::alert(app(Project::class));
$playbook = Playbook::make($project->template);
return $playbook->getPlays()->map(function (Play $play) {
//---
});
}
The first Debug shows the correct Model instance, but the second one shows a blank instance. Am I going crazy, or am I completely misunderstanding how all this is meant to work?
32 replies
FFilament
Created by Malcolm Turntbull on 11/14/2023 in #❓┊help
Does Filament change Laravel's default Route Model Binding?
Maybe there is magic in the Resource that allows this to work, and it's not using RMB at all?
32 replies