Maybe you could try doing a raw query using REGEXP_REPLACE or REGEXP_SUBSTR with an alias, and then ordering by that alias.
ref: https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace
Hope this helps!
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone,
I'm working on a project where I'm retrieving a collection. The model I'm retrieving has a property url where an url is stored. This url can be something like https://laracasts.com or http://laracast.com or https://www.laracasts.com.
I am ordering and paginating the collection using $models->orderBy('url', 'ASC')->paginate(); which is working as expected however I would like to order my collection on the url field whilst ignoring the http or https protocol and www subdomain. It is essential that pagination will still work.
Could anyone offer me some advise on how to accomplish this? Thanks in advance :)!
@nilsringersma try this sample query directly on your database and see if it's what you may be needing:
SELECT REGEXP_REPLACE('https://www.laracasts.com','http://|https://|www.','',1,0,'c') as URL order by URL;
In your case you can replace 'https://www.laracasts.com' with the url field of your table.
Here you have a working example: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=92920e3ec2af40fc82283fed80d8e420
Let me know if this worked for you!
Please or to participate in this conversation.