Php 有了Laravel,我如何在迁移表中组织城市,以便为它们分配唯一的slug来过滤商业列表?

Php 有了Laravel,我如何在迁移表中组织城市,以便为它们分配唯一的slug来过滤商业列表?,php,mysql,laravel,Php,Mysql,Laravel,tl;dr:我如何组织我的数据库表,以便能够像justia.com/layers网站那样,将州和城市名称转换为一个独特的slug? 一位客户让我在一个律师名录网站上工作,他们希望用户能够根据他们的执业区域(类别)、城市和州或邮政编码找到律师。为了使搜索引擎优化友好的网址,我计划模仿一个非常类似的网站,它有以下网址结构(政变 为了实现这一点,我创建了以下表格,并计划在保存elloquent模型时使用sluggable laravel包自动生成Slug: Schema::create('lawyer

tl;dr:我如何组织我的数据库表,以便能够像justia.com/layers网站那样,将州和城市名称转换为一个独特的slug?

一位客户让我在一个律师名录网站上工作,他们希望用户能够根据他们的执业区域(类别)、城市和州或邮政编码找到律师。为了使搜索引擎优化友好的网址,我计划模仿一个非常类似的网站,它有以下网址结构(政变

为了实现这一点,我创建了以下表格,并计划在保存elloquent模型时使用sluggable laravel包自动生成Slug:

Schema::create('lawyers', function (Blueprint $table) {
      $table->increments('id');
      $table->string('first_name', 50);
      $table->string('middle_name', 50)->nullable();
      $table->string('last_name', 50);
      $table->string('email', 256)->unique();
      $table->string('phone', 15);
      $table->string('website',200);
      $table->string('company',100);
      $table->date('licensed_since');
      $table->timestamps();
});

Schema::create('cities', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name', 16);
    $table->integer('zip');
    $table->string('state', 2);
    $table->decimal('lat', '8', '6');
    $table->decimal('long', 10, 6);
    $table->string('slug')->unique();
    $table->timestamps();
});


 Schema::create('practice_areas', function (Blueprint $table) {
      $table->increments('id');
      $table->string('practice_area', 200);
      $table->string('slug')->unique();
});

/*------------------------ Pivot Tables ------------------------*/

Schema::create('city_lawyer', function (Blueprint $table) {
      $table->integer('lawyer_id')->unsigned();
      $table->foreign('lawyer_id')->references('id')->on('lawyers');
      $table->integer('city_id')->unsigned();
      $table->foreign('city_id')->references('id')->on('cities');
});

Schema::create('lawyer_practice_area', function (Blueprint $table) {
      $table->integer('lawyer_id')->unsigned();
      $table->foreign('lawyer_id')->references('id')->on('lawyers');
      $table->integer('practice_area_id')->unsigned();
      $table->foreign('practice_area_id')->references('id')->on('practice_areas');
});

/*------- Not a part of the issue but here for reference  -------*/

Schema::create('addresses', function (Blueprint $table) {
      $table->increments('id');
      $table->string('address1', 100);
      $table->string('address2', 50);
      $table->timestamps();
});

Schema::create('address_lawyer', function (Blueprint $table) {
      $table->integer('lawyer_id')->unsigned();
      $table->foreign('lawyer_id')->references('id')->on('lawyers');
      $table->integer('address_id')->unsigned();
      $table->foreign('address_id')->references('id')->on('addresses');
});
到目前为止,网站的practice_area部分运行良好。但我在尝试对城市进行此操作时遇到了问题,因为某些城市名称多次列在城市表中,因为一些城市有多个邮政编码,不同的州通常共享城市名称


所以我的问题是,组织数据库的最佳方式是什么,这样我就可以像justia.com/lawyers网站那样将城市和州作为slug使用?

我的一个想法是将城市表中的slug设置为“state/city”而不仅仅是城市的鼻涕虫,将邮政编码移动到一个新表中,这样就形成了一个一对多的关系。我能有一个laravel路由,其中变量有一个斜杠吗?(而不是一个分隔符)是的,不是,有点。你不能在url中使用
/
来表示除分隔符以外的内容,尽管你可以传递2个参数,或者让第二个参数作为可选参数,并进行相应的检查和解析。
Schema::create('lawyers', function (Blueprint $table) {
      $table->increments('id');
      $table->string('first_name', 50);
      $table->string('middle_name', 50)->nullable();
      $table->string('last_name', 50);
      $table->string('email', 256)->unique();
      $table->string('phone', 15);
      $table->string('website',200);
      $table->string('company',100);
      $table->date('licensed_since');
      $table->timestamps();
});

Schema::create('cities', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name', 16);
    $table->integer('zip');
    $table->string('state', 2);
    $table->decimal('lat', '8', '6');
    $table->decimal('long', 10, 6);
    $table->string('slug')->unique();
    $table->timestamps();
});


 Schema::create('practice_areas', function (Blueprint $table) {
      $table->increments('id');
      $table->string('practice_area', 200);
      $table->string('slug')->unique();
});

/*------------------------ Pivot Tables ------------------------*/

Schema::create('city_lawyer', function (Blueprint $table) {
      $table->integer('lawyer_id')->unsigned();
      $table->foreign('lawyer_id')->references('id')->on('lawyers');
      $table->integer('city_id')->unsigned();
      $table->foreign('city_id')->references('id')->on('cities');
});

Schema::create('lawyer_practice_area', function (Blueprint $table) {
      $table->integer('lawyer_id')->unsigned();
      $table->foreign('lawyer_id')->references('id')->on('lawyers');
      $table->integer('practice_area_id')->unsigned();
      $table->foreign('practice_area_id')->references('id')->on('practice_areas');
});

/*------- Not a part of the issue but here for reference  -------*/

Schema::create('addresses', function (Blueprint $table) {
      $table->increments('id');
      $table->string('address1', 100);
      $table->string('address2', 50);
      $table->timestamps();
});

Schema::create('address_lawyer', function (Blueprint $table) {
      $table->integer('lawyer_id')->unsigned();
      $table->foreign('lawyer_id')->references('id')->on('lawyers');
      $table->integer('address_id')->unsigned();
      $table->foreign('address_id')->references('id')->on('addresses');
});