10

Looking at Str::slug for my frontend URL generation but just wondering how you guys go about implementing it with routes etc, for example, how would you guys go about changing http://www.example.com/courses/1 to http://www.example.com/courses/this-course

Gareth Daine
  • 4,016
  • 5
  • 40
  • 67

6 Answers6

12

OK, I did it this way:

// I have a slug field in my courses table and a slug field in my categories table, along with a category_id field in my courses table.

// Route 

Route::get('courses/{categorySlug}/{slug?}', function($categorySlug, $slug) {
    $course = Course::leftJoin('categories', 'categories.id', 'courses.category_id')
        ->where('categories.slug', $categorySlug)
        ->where('courses.slug', $slug)
        ->firstOrFail();

    return View::make('courses.show')->with('course', $course);
});

Works like a charm. It gets the $categorySlug and $slug variables then uses them to filter the Eloquent model Course to get the correct course object from the database.

EDIT: You can generate a URL in your view like:

http://www.example.com/courses/it-training/mcse

By doing something like:

<a href="{{ URL::to('courses/'.$course->category->parentCategorySlug($course->category->parent_id).'/'.$course->category->slug.'/'. $course->slug) }}" title="{{ $course->title }}">{{ $course->title }}</a>

A have a method in my Category like below that retrieves the parent category slug. This could be better achieved though using some sort of presenter class which would allow you to simply use $course->url but I haven't got around to doing this yet. I will update the answer when I do.

public function parentCategorySlug($parentId)
{
    if ($parentId === '0')
    {
        return $this->slug;
    }

    return $this->where('id', $parentId)->first()->slug;
}
Gareth Daine
  • 4,016
  • 5
  • 40
  • 67
5

You can use the cvierbrock's Eloquent-Sluggable package.

cawecoy
  • 2,359
  • 4
  • 27
  • 36
4

As for me I created a helper function and used the following method taken from here.

 public static function getSlug($title, $model) {
    $slug = Str::slug($title);
    $slugCount = count( $model->whereRaw("url REGEXP '^{$slug}(-[0-9]*)?$'")->get() );
    return ($slugCount > 0) ? "{$slug}-{$slugCount}" : $slug;
}
Mr. Crowley
  • 3,225
  • 4
  • 25
  • 28
2

You can create a related model Slug, and approach the course in your methods like so:

$course = Slug::where('slug', $slug) -> firstOrFail() -> course;
reshetech
  • 853
  • 7
  • 10
1

I have also implemented a similar URL mapping but I preferred to have both the ID and the slug in the requested URL, like this:

http://www.example.com/courses/1/my-laravel-course

This method allows me to get the requested course object from the ID given in the URL, rather than having to store the slugs in my DB table.

Route::post('courses/(:num)/(:any)', function ($courseid, $slug) {
    $course = Course::where('id', '=', $courseid)->get();
    return View::make('courses.show')->with('course', $course);
}
Spooky
  • 2,966
  • 8
  • 27
  • 41
deepika jain
  • 363
  • 3
  • 7
0

For Laravel 8:

Given my URL:

http://www.example.com/courses/this-course

My route:

Route::get('/courses/{course:slug}' , function(Course $course){
     return view('showCourse' , [
         'course' => $course
     ])
})
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77