【问题标题】:use slugs instead of id使用 slugs 而不是 id
【发布时间】:2019-07-01 15:43:52
【问题描述】:

我用 laravel 5.8 开发了一个网站,我想使用 slugs 而不是 id。手提包知道我有两个表服务和类型服务。服务键在服务类型表中是外来的。

我按服务类型检索和显示服务。但在我的 url 上显示了类型 ID。

我有这个错误

缺少 [Route: service] [URI: service/{slug}] 的必需参数。 (查看:C:\laragon\www\elbi\resources\views\layouts\partial\nav.blade.php) (查看:C:\laragon\www\elbi\resources\views\layouts\partial\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>

我想使用 slug 而不是 id

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你可以试试Spatie's Laravel-Sluggable 包裹。

    $service = new Service();
    $service->name = 'This is a service';
    $service->save();
    
    echo $service->slug; // ouputs "this-is-a-service"
    

    【讨论】:

      【解决方案2】:

      您可以通过将slug 存储在数据库中并相应地修改控制器中的查询来做到这一点, Slug 可以在保存模型之前设置如下:

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

      您可以在调用 save 方法或使用 Eloquent events - 特别是 saving 事件之前手动放置此逻辑。

      【讨论】:

      • 您的回答不完整/错误。 \Str::slug 导致错误 Class 'Str' not found
      • 我更新了 Str 类的命名空间,它是 laravel 助手之一 (laravel.com/docs/5.8/helpers#method-str-slug)
      • 我想补充一点,如果config/app.php 中的别名正确,\Str::slug() 应该可以在不定义其命名空间或导入它的情况下工作。您可能仍需要导入Str。例如。 use Str;.
      猜你喜欢
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 2015-04-16
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      相关资源
      最近更新 更多