Я разрабатываю сайт с laravel 5.8 и хотел бы использовать слагы, а не id. тотчас зная, что у меня есть два табличных сервиса и type-сервисы. служебный ключ является внешним в таблице типов услуг.
Я получаю и отображаю услуги по типу службы. но на моем URL отображается идентификатор типа.
У меня есть эта ошибка
Отсутствуют обязательные параметры для [Route: service] [URI: service / {slug}]. (Представление: C: \ laragon \ www \ elbi \ resources \ views \ layouts \partal \ nav.blade.php) (Представление: C: \ laragon \ www \ elbi \ resources \ views \ layouts \partal \ nav.blade. PHP)
таблицы:
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();
});
}
Модель
<?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';
}
}
контроллер дисплея
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'));
}
Маршрут
/*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');
вид
<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>
Я бы хотел использовать слаг, а не id