Describe the bug
When creating a relationship on a table that references other records of the same table, the routes generated use the entity name rather than the relationship name. For instance, in the application I'm currently building, I have a "pests" table for defining various insects. Some insects are predators, some insects are prey, and some insects are both. When creating a new insect, the following LogicException appears:
Route pattern "/api/pests/{pest}/pests/{pest}" cannot reference variable name "pest" more than once.
There is also only a single API controller created for handling the requests: PestPestsController, rather than 2 controllers: PestPredatorsController and PestPreyController.
My example is fairly specific, however I could see this being an issue with more common scenarios such as creating taxonomies with parent/child relationships.
To Reproduce
Steps to reproduce the behavior:
Create an entity called "Pest"
Add a "belongsToMany" relationship, using the following settings:
a. Relationship Name: "predators"
b. Pivot Table Name: "predator_prey"
c. Local Model Key Name: "predator_id"
d. Joined Model Key Name: "prey_id"
Once saved, an additional "belongsToMany" relationship is automatically created named "pests", edit this relationship as follows:
First input: "prey"
Second input: "prey_id"
Third input: "predator_id"
Generate the code, run migrations, and open in the browser.
Create a new pest. Once saved, an error will appear:
Route pattern "/api/pests/{pest}/pests/{pest}" cannot reference variable name "pest" more than once.
Expected behavior
For relationships, I believe the route keys should use the relationship name rather than the entity name, and add the proper binding to the boot method of the RouteServiceProvider if the relationship name is customized as shown in the docs. E.g.:
Finally, there should be 2 controllers generated: PestPredatorsController and PestPreyController
// PestPredatorsController.php
<?php
namespace App\Http\Controllers\Api;
use App\Models\Pest;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use App\Http\Resources\PestCollection;
class PestPredatorsController extends Controller
{
public function index(Request $request, Pest $prey): PestCollection
{
$this->authorize('view', $prey);
$search = $request->get('search', '');
$pests = $prey
->predators()
->search($search)
->latest()
->paginate();
return new PestCollection($pests);
}
public function store(Request $request, Pest $prey, Pest $predator): Response
{
$this->authorize('update', $prey);
$prey->predators()->syncWithoutDetaching([$prey->id]);
return response()->noContent();
}
public function destroy(Request $request, Pest $prey, Pest $predator): Response
{
$this->authorize('update', $prey);
$prey->predators()->detach($prey);
return response()->noContent();
}
}
// PestPreyController
<?php
namespace App\Http\Controllers\Api;
use App\Models\Pest;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use App\Http\Resources\PestCollection;
class PestPreyController extends Controller
{
public function index(Request $request, Pest $predator): PestCollection
{
$this->authorize('view', $predator);
$search = $request->get('search', '');
$pests = $predator
->prey()
->search($search)
->latest()
->paginate();
return new PestCollection($pests);
}
public function store(Request $request, Pest $predator, Pest $prey): Response
{
$this->authorize('update', $predator);
$pest->prey()->syncWithoutDetaching([$predator->id]);
return response()->noContent();
}
public function destroy(Request $request, Pest $predator, Pest $prey): Response
{
$this->authorize('update', $predator);
$predator->prey()->detach($predator);
return response()->noContent();
}
}
Desktop (please complete the following information):
OS: application is installed on host machine: macbook pro 2021 M1 Max,
Describe the bug When creating a relationship on a table that references other records of the same table, the routes generated use the entity name rather than the relationship name. For instance, in the application I'm currently building, I have a "pests" table for defining various insects. Some insects are predators, some insects are prey, and some insects are both. When creating a new insect, the following
LogicException
appears:Route pattern "/api/pests/{pest}/pests/{pest}" cannot reference variable name "pest" more than once.
There is also only a single API controller created for handling the requests:
PestPestsController
, rather than 2 controllers:PestPredatorsController
andPestPreyController
.My example is fairly specific, however I could see this being an issue with more common scenarios such as creating taxonomies with parent/child relationships.
To Reproduce Steps to reproduce the behavior:
Route pattern "/api/pests/{pest}/pests/{pest}" cannot reference variable name "pest" more than once.
Expected behavior For relationships, I believe the route keys should use the relationship name rather than the entity name, and add the proper binding to the boot method of the RouteServiceProvider if the relationship name is customized as shown in the docs. E.g.:
https://laravel.com/docs/10.x/routing#explicit-binding
The routes should be generated as follows:
Finally, there should be 2 controllers generated:
PestPredatorsController
andPestPreyController
Desktop (please complete the following information):