Journal & Notebook

Laravel Example: Spark Repositories

I've been perusing the code for Laravel Spark both for inspiration and to see how Taylor Otwell writes his application code. One class that took my special interest was the API\InvitationController.

Here are its dependencies.

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Laravel\Spark\Contracts\Repositories\TeamRepository;

Notice that the only repository dependency for the InvitationController is TeamRepository. So, how do we get an Invitation result when it is not specified as a dependency above? I have added inline comments to make it clear.

While this is an interesting technique to avoid a use statement (and maybe another repository injection), it takes three lines to figure out what the invitation model is. In the end, the code did get the job done. However, I wonder if it would've been clearer to inject an InvitationRepository to make the code more readable in the Controller (even a use statement for the Invitation model).

I must say that the visibility rules for the model relationships are very cool. This makes it easy to identify exactly what will be returned in the relationship json for this API controller.