Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

YuraLons's avatar

Laravel does not open the required page in the category

If you try to open the category (tables) and select (Red table with white borders) in it, then such a page will open without problems and information will be on this table, but if you open in the same category (tables), another table, for example (Blue table with white inserts), then again the red table will open, but the path to the blue table will open in the browser address bar. This is the information when I go to the red table Using the dump command I get the following result:

"id" => 1
    "title" => "Red table with white borders"
    "alias" => "red-table-with-white-borders"
    "img_path" => "none"
    "description" => "fuf"
    "created_at" => "2024-01-20 16:23:11"
  ]
    "categories" => 
            "id" => 1
            "title" => "Table Gaming"
            "alias" => "table-gaming"
            "pivot_service_id" => 1
            "pivot_category_id" => 1

And exactly the same information when I go to the blue table I am attaching the source data in the controller in the view and in the paths

ItemController.php

/**
   * @param $catname
   * @return Application|Factory|View|\Illuminate\Foundation\Application
   */
  public function getCategoriesItem($catname)
  {
    $data = Category::where('alias', $catname)->with('services')->firstOrFail();
    if (!$data){
      abort(404, 'Page not found');
    }
    return view('template.sait.page.categories.show', compact('data'))->with('alias', $catname);
  }
  public function getServicesFormCategory($alias)
  {
    $items = Service::whereHas('categories', function ($categories) 
    use($alias)
    {
      $categories->where('alias', $alias);
    })
      ->with('categories')
      ->with('doctors')
      ->first();
    if (!$items){
      abort(404, 'Page Not found');
    }
    dd($items);
    // return view('template.sait.page.categories.show.view', compact('items'))->with('alias', $alias);

web.php

Route::get('categories/{catname}', [ItemController::class, 'getCategoriesItem'])->name('www.categories.show');
Route::get('categories/{catname}/{alias}', [ItemController::class, 'getServicesFormCategory'])->name('www.categories.services.show');

view (Blade)

<div class="flex flex-row flex-wrap gap-3 justify-between">
      @foreach($data->services as $link)
        <a 
        class="tw-btn-info" 
        href="{{route('sait.categories.get.services', ['catname' => $data->alias, 'alias' => $link->alias])}}">
        {{$link->title}}</a>
      @endforeach
    </div>
0 likes
3 replies
kiwi0134's avatar

I'm not entirely sure what your exact issue is. Can you please explain it a bit differently or even show screenshots / a screencast?

Either way, I'd like advise you to use route model binding for to get rid of that boiler plate of loading and checking for a model. You can load additional relationships later:

public function show(Category $category) {
    $category->load('services');
}
YuraLons's avatar

@kiwi0134 I need to make sure that when I go to the Categories page, I can select one of the categories, and by going to it I get a list of all the objects that are there, then I need to select any object and open it directly, and the url should be such a plan www.domain.app/categories/table/table-pc-dark. At the moment I can get a list of all objects in a category, but when I go to any of them I get the first object inside the category by link, that is, I go to the link www.domain.app/categories/table/table-pc -dark and I open an object with fields from the object ..../table/table-pc

kiwi0134's avatar

@YuraLons I understood that you want to build a CRUD application with nested data. Alá:

  1. User can view all product categories
  2. User can navigate to a category to see the products inside that category
  3. The user can navigate to a product inside a category and see product details

This is farily common and pretty easy to implement. I won't give you the whole code, as that would blow my answer, but here's a pseudocode outline of one way you could do that in a standard-layout Laravel application:

# Routes (web.php)
Route::get('categories', [CategoryController::class, 'index']);
Route::get('categories/{category}', [CategoryController::class, 'show']);

// You probably want to scope your product to your category.
// read: https://laravel.com/docs/10.x/routing#implicit-model-binding-scoping
Route::get('categories/{category}/{product}', [CategoryProductController::class, 'show'])->scopeBindings();

# CategoryController
function index() {
    $categories = // get your categories, probably paginated, sorted, whatever you want

    return view('categories.index', [
        'categories' => $categories
        # (I like to make it really explicit and try to avoid the compact method)
    ]);
}

// By naming the variable exactly like the placeholder inside the route definition,
// Laravel automatically provides the model or fails with a 404, if the model does
// not exist by that key. Please have a look at the documentation if you want
// to use a different column than the ID for route model binding:
// https://laravel.com/docs/10.x/routing#customizing-the-key
function show(Category $category) {
    // Load the relationships beforehand to prevent the N+1 problem
    $category->load('products');

    // You can also paginate the products
    $products = $category->products->paginate();

    return view('categories.index', [
        'category' => $category,
        'products' => $products
        // (I like to make it really explicit and try to avoid the compact method)
    ]);
}

# CategoryProductController
function show(Category $category, Product $product) {
    // basically the same as above, 
    // but return the view and data for the product instead
}

You should get an idea about how this works. I can definitely recommend to go through the documentation. It's a fairly quick read, if you read about the topics you need, when you need them and it helps a lot with understanding the framework.

Please or to participate in this conversation.