MooseSaid's avatar

Why Str::slug doesn't work?

I want my Laravel application to create a slug url for my product names and when a product has the same slug, it addes number, letters or whatever.

I can do it by looping over the table and if found any slug with same value it can add to the slug or something, but what I know is that Str::slug should do that for me so why isn't it working anymore?

Laravel docs isn't updated in that section as it has Laravel 5 example

0 likes
9 replies
MooseSaid's avatar

@oussamamater Yes it does the trick and slug-fy my string but if I have the same slug in database it doesn't work. It creates the same slug and I end up with the same url for 2 products. I didn't provide any errors because there isn't any. The only error I get is something silly I tried but It's the reason why It made me chase an answer instead of using any package.

So I was trying to make a rule where a product can't have same slug in the same shop. So I ended up doing this in my products migration file.

$table->unique(['slug', 'webshop_id'], 'slug_webshop_unique');

I want to say that a shop can't have a product with the same slug. And then I went on searching on how to make the slug change itself when it find another slug with the same slug value.

So I stumbled on this thread.

To summerize, It should do something like this i-am-a-unique-slug and the second slug with the same slug value would be i-am-a-unique-slug-1 and so on.

@tykus Said that Illuminate/Support/Str was never responsible for achieving this but the error I got is the reason why I wasted hours instead of using a package to solve my issue :(

 Integrity constraint violation: 1062 Duplicate entry 'my-first-title-1' for key 'products.slug_webshop_unique' (SQL: insert into `products` (`user_id`, `webshop_id`, `category_id`, `main_thumbnail`, `title`, `slug`, `price`, `currency`, `type`, `desc`, `qty`, `updated_at`, `created_at`) values (1, 1, ?, thumbnails/25igqD5KJeqItGI4CqlPJhDM6kK66jD0edS2dDje.jpg, my first title, my-first-title, 12, usd, physicalProd, ?, ?, 2022-09-05 18:53:05, 2022-09-05 18:53:05))

As You can see the my-first-title-1 is right there because another product has the same slug, but for some reasone the SQL query trying to insert the normal slug.

trying to Str::slug($validated['title'], '-') gets me the normal slug not the modified slug.

Snapey's avatar

@MooseSaid The error can only be in your code as you are responsible for inserting the records.

The Str::slug method ONLY converts the string

The validation ONLY checks if there is already a slug in the database with the suggested name

YOU (or a package) are responsible for creating the unique slug and inserting it into the database

1 like
tykus's avatar

@MooseSaid the slug in the error message is not the slug being inserted, it refers also to the webshop_id; because, as you can see in the SQL INSERT statement, it has my-first-title as the slug!

If you want to check the database for every possible valid candidate slug, then have at it... but the package(s) already solves this problem.

1 like
MooseSaid's avatar

@tykus @snapey Got it, it's not Str::slug responsibility. Final question, is it worth it to use a package? isn't the whole thing as simple as (loop over products table, if there is a slug with same value then add '-1' or something) and so on?

Snapey's avatar

@MooseSaid pretty much. There are different strategies that could be used but basically keep trying until there are no existing matches.

1 like

Please or to participate in this conversation.