1

I'm working on my first Laravel 4 project and using the eloquent ORM to retrieve rows from the database using a route. Example:

Here is a route:

Route::get('{publisher}/{series}', function($publisher, $series)
{
$result = Comic::where('publisher', '=', $publisher)
->where('series', '=', $series)
->orderBy('issue', 'asc')
->get();

return View::make('comic')
->with('result', $result); 
});

This is supposed to be matching a url like site.com/marvel/amazing-spider-man

Right now it only works for site.com/marvel/amazing spider-man or site.com/marvel/amazing%20spider-man

How can I make sure to only use the '-' urls? I believe what I need to do is:

a. write code that replaces the '-' with a %20 during the routing b. write code that replaces a %20 with a '-' during url generation

Another consideration is that I don't really want my content to be accessible on both urls.

1
  • The character you are referring to is the minus character. As dash is a different character which is longer. Commented Feb 14, 2019 at 10:16

4 Answers 4

5

The reason why I think you are not getting the result you expect is because of the format in which the "series" name is stored in the database, remember that is looking for an exact match, so if you want to retrieve the results for:

http://www.site.com/marvel/amazing-spider-man

You will need to store it in the database as "amazing-spider-man" to avoid the extra step of removing dashes (-), if you don't like this format then you can do:

$series = str_replace('-', ' ', $series); 

This will remove the dashes, but beware not to use dashes within the names because they will get removed, and now you can store the series name in the database as "amazing spider man".

Sign up to request clarification or add additional context in comments.

1 Comment

This is a good idea. The problem is the database has both types of titles, some are actually formally titled with dashes in them and some are not. The database contains about 200,000 rows and it would be a total pain to re-do the entire thing. Would another solution be to utilize an ID of some sort in the url like sitecom/marvel/3/amazing-spider-man where the id would map to the database only?
2

->with('result', Str::slug($result));

  • Str::slug(string $title, string $separator = '-')

http://laravel.com/docs/4.2/helpers#strings

Comments

0

As of 2021 this is the first Google result to this kind of question and some people may appreciate my solution to this case since this is still relevant nowadays.

I have used a public *.csv file to save countries and cities in my database. Therefore there are entries with dashes AND spaces like OP's issue.

Also, like OP mentioned in his comment he would have to rename all of his database entries.

Therefore I have a good solution to this question:

  1. Make sure all of your links will have an underscore instead of a space for more readability (improves SEO also) like:

$slug = str_replace(' ', '_', $this->country); // "United%20States" will be "United_States"

  1. Open up your MySQL console and replace every space with an underscore with this command:

UPDATE locations SET country = replace(country, ' ', '_');

This way entries with a hyphen (-) will still persist in your database and all other entries with spaces will have an underscore to be exactly matched. You have also improved SEO and URL readability.

Comments

0

This is the solution for Laravel.

use Str;
use Carbon\Carbon;

$start_at = Str::replace('-', '/', '12-28-2021');
Carbon::parse($start_at);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.