0

I have following function in my controller

public function showSubCats($categoryId) {

   $subcats = DB::table('sub_category as sc')
    ->leftJoin('products as p', 'p.sub_cat_id', '=', 'sc.sub_cat_id')
    ->where('sc.category_id', '=', $categoryId)
    ->whereNotNull('p.sub_cat_id')
    ->select('p.*','sc.*', DB::raw('sc.sub_cat_id AS sub_cat_id'))
    ->groupBy('sc.sub_cat_id')
    ->get();

     return View::make('site.subcategory', [            
          'subcats' => $subcats

     ]); 
}

This in the router

Route::get('/category/{categoryId}', ['uses' => 'CategoryProducts@showSubCats']);

And the button

<a href="{{ URL::to( '/category/' .  $category->category_id) }}">View More</a>

Currently the url looks like - http://example.com/category/1 where 1 is the ID. I want to show the name instead.

I easily can make something like

Route::get('/category/{categoryId}/{name}', ['uses' => 'CategoryProducts@showSubCats']);

button

<a href="{{ URL::to( '/category/' .  $category->category_id.'/'.$category->category_name) }}">View More</a>

But the ID will be still there. I can't also pass only the name of the category because if you check above query I need the ID because I'm joining tables via ID's.

Any idea how can I do this?

Laravel version 4.2

VLS
  • 445
  • 2
  • 6
  • 25

1 Answers1

1

You don't need $categoryId for the join, because you only use it in your where statement. It is possible to change this line:

->where('sc.category_id', '=', $categoryId)

into

 ->where('sc.category_name', '=', $categoryName)

You wouldn't need to pass the id to showSubCats anymore, and can replace it with $categoryName.

This only works if the category name is unique. Otherwise you would still need the id.

In that case, you can hide the id, by using a query in the route, that fetches the id of the subcategory, based on the name. Try something like this:

Route::get('/category/{categoryName}', function($categoryName) {

    $categoryId = DB::select('select id from sub_category where name = ?', array($categoryName))[0]->id;

    return redirect()->action(
         'CategoryProducts@showSubCat', ['id' => $categoryId]
    );
});
piscator
  • 8,028
  • 5
  • 23
  • 32
  • Thanks for the answer. Thats what I was going to ask - what will happen if the name isn't unique? Normally will be .. but there is possibility to have category or sub-category with same name. – VLS Mar 27 '17 at 11:57
  • Actually, in your query I'm not sure if it really would be a problem, since I don't know what the desired results of your query are. Do you want to show the subcategories of a category? – piscator Mar 27 '17 at 12:04
  • There is another minor query also in this function which isn't much relevant to this and I've removed it to shorten a bit question. Bottom line is that I need to pass ID. And yes, I show sub-categories of category and products also which are assigned only to this category. – VLS Mar 27 '17 at 12:09
  • Since you filter the subcategories in the join, I don't expect you will have any trouble with duplicates. But you have to run some tests, because I don't now what your data model is. You could also consider to use Eloquent instead of the DB class and raw queries. It would make you able to show the relations of the sub categories (products and categories)., filter out a certain product, etc. – piscator Mar 27 '17 at 12:25
  • Is there a way to not pass the name as parameter but still get it at the url? – VLS Mar 27 '17 at 12:27
  • In this answer, a post request or using encryption is suggested: http://stackoverflow.com/a/39952127/4593376 – piscator Mar 27 '17 at 12:54
  • I need something like this I believe https://laravel.io/forum/03-11-2014-how-to-make-urls-seo-friendly-with-nested-set-model – VLS Mar 27 '17 at 13:08
  • I've added an update for you, with another suggestion. – piscator Mar 27 '17 at 13:29
  • So I should put this in my route.php.. do I need any changes to button? – VLS Mar 27 '17 at 13:55
  • I've got `Undefined variable: categoryName` here -> `$categoryId = DB::select('select id from sub_category where name = ?', array($categoryName))[0]->id;` – VLS Mar 27 '17 at 14:01
  • That's correct, I forgot to add the $categoryName parameter to the function in the route, in my example. – piscator Mar 27 '17 at 14:17
  • Now on the same line I've got `Undefined offset: 0` – VLS Mar 27 '17 at 14:21
  • Try a dd() of the select statement. This will help you solve the issue. Good luck! – piscator Mar 27 '17 at 14:42