Arsene
Arsene

Reputation: 1

use slugs instead of id

I develop a site with laravel 5.8 and I would like to use slugs rather than id. tote knowing that I have two table services and type-services. the service key is foreign in the service-type table.

I retrieve and display the services by the type of service. but on my url the type id is displayed.

I have this error

Missing required parameters for [Route: service] [URI: service/{slug}]. (View: C:\laragon\www\elbi\resources\views\layouts\partial\nav.blade.php) (View: C:\laragon\www\elbi\resources\views\layouts\partial\nav.blade.php)

the tables:

 public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }


 public function up()
    {
        Schema::create('type_services', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('service_id');
            $table->foreign('service_id')->references('id')->on('services')->onDelete('cascade');
            $table->string('name');
            $table->text('description');
            $table->string('image');
            $table->timestamps();
        });
    }

Models

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Service extends Model
{
    public function typeservice()
    {
        return $this->hasMany('App\Typeservice');
    }


}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TypeService extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function service()
    {
        return $this->belongsTo('App\Service');
    }
    public function getRouteKeyName()
    {
        return 'slug';
    }
}

display controller

 public function index()
    {
        $contacts = Contact::all();
        $abouts = About::all();
        $services = Service::with('typeservice')->orderBy('name','asc')->get();
        $typeservices =TypeService::all();
        $makings = Making::all();
        $realisations = Realisation::all();
        $addresses = Address::all();
        return view('index',compact('contacts','services','abouts','typeservices','makings','realisations','addresses'));
    }

 public function service(Service $service)
    {
        $abouts = About::all();
        $services = Service::with(['typeservice'=> function($query){
            $query->where('name');
        }])->orderBy('name','asc')->get();
        $typeservices =$service ->typeservice()->where('service', function ($query) {
            $query->where('name');
        })->paginate(5);;
        $makings = Making::all();
        $realisations = Realisation::all();
        return view('service.service',compact('services','abouts','typeservices','makings','realisations'));
    }

Route

/*controller front */
Route::get('/','FrontController@index')->name('index');
Route::get('/contact','FrontController@send')->name('send');
Route::post('/contact','ContactController@sendMessage');
Route::get('/about','FrontController@about')->name('about');
Route::get('/service/{slug}', 'FrontController@service')->name('service');

view

   <li class="nav-item submenu dropdown">
            <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Services</a>
            <ul class="dropdown-menu">
                @foreach($services as $service)
                <li class="nav-item"><a class="nav-link" href="{{route('service',$service->id)}}">{{$service->name}}</a></li>
                @endforeach
            </ul>
        </li>

I would like to use slug and not id

Upvotes: 0

Views: 2190

Answers (2)

Zaid Rashwani
Zaid Rashwani

Reputation: 11

You can do that by storing slug in the database and modify your queries in the controller accordingly, Slug can be set before saving the model as below:

$slug = \Illuminate\Support\Str::slug($service->name);
$service->slug = $slug;

you can place this logic manually before calling save method or use Eloquent events - specifically saving event .

Upvotes: 1

stokoe0990
stokoe0990

Reputation: 473

You could try giving Spatie's Laravel-Sluggable package a go.

$service = new Service();
$service->name = 'This is a service';
$service->save();

echo $service->slug; // ouputs "this-is-a-service"

Upvotes: 3

Related Questions