#Controller shallow nesting

3 messages · Page 1 of 1 (latest)

nova bluff
#

Hello,

How is supposed to be managed the index method in shallow nesting?

Route::resource('videos.comments', CommentController::class)->shallow();
Route::resource('photos.comments', CommentController::class)->shallow();

class CommentController extends Controller
{
  public function index()
  {
    // How to handle here the binded model? (Video or Photo)
  }
}

And how is it supposed to make a different representation depending on the model?

class CommentController extends Controller
{
  public function index()
  {
    // If the binded model is Video, the builder columns, filters and relationships are different from Photo model
  }
}

I'm totally missed in this shallow approach 🥲

sleek shale
#

Do you mean something like this?

Route::resource(
  '{parent_type}/{parent_id}/comments', 
  CommentController::class,
)->name('comments');
#
class CommentController extends Controller
{
  public function index(Request $request, $parentType, $parentId)
  {
    $parentModel = collect(Relation::morphMap())->get($type);
    $parent = $parentModel::find($parentId);
    $comments = $parent->comments;
    // ...
  }
}