Php 如何向Laravel发送参数?

Php 如何向Laravel发送参数?,php,laravel,eloquent,routes,Php,Laravel,Eloquent,Routes,我有医生和乡村医生 Schema::create('doctors', function (Blueprint $table) { $table->increments('id'); $table->integer('doctor_no')->unique(); $table->string('slug')->unique(); $table->string('fullname')->uni

我有医生和乡村医生

Schema::create('doctors', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('doctor_no')->unique();
        $table->string('slug')->unique();
        $table->string('fullname')->unique();
        $table->string('profile_image')->nullable();
        $table->date('birthday')->nullable();
        $table->enum('gender', ['male', 'female'])->nullable();
        $table->string('phone');
        $table->string('email');
        $table->string('password');
        $table->string('website_url')->nullable();
        $table->smallInteger('starting_month')->unsigned()->nullable();
        $table->year('starting_year')->nullable();
        $table->rememberToken();
        $table->timestamps();
        $table->dateTime('sort_date');
        $table->softDeletes();
    });


Schema::create('doctor_country_city', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('doctor_id');
        $table->unsignedInteger('country_id');
        $table->unsignedInteger('city_id');
        $table->timestamps();
        $table->foreign('doctor_id')->references('id')->on('doctors');
        $table->foreign('country_id')->references('country_id')->on('cities');
        $table->foreign('city_id')->references('id')->on('cities');
    });
我想向路由发送多个参数。我的控制器文件和模型关系应该是什么样的

Example: Route::get('{country?}/{city?}/{slug?}', 'DoctorsController@showDoctor');

在给出解决方案之前,我建议您重新考虑数据库设计

一个医生能来自许多城市吗?如果没有,您首先就不需要
doctor\u country\u city
pivot表。你也不应该把医生和一个城市和一个国家联系起来。这很奇怪,因为您的数据库可能允许将医生分配到法国纽约。根据定义,一个城市只属于一个国家

所以我宁愿把一个医生和一个城市联系起来,而这个城市和一个国家联系起来

johndoe博士>纽约>美国

这对我来说更有意义。因此,您将有一个名为
countries
的表,它们的模型将具有如下关系:

class Doctor extends Model {
   public function city() {
      return $this->belongsTo(City::class);
   }
}

class City extends Model {
   public function country() {
      return $this->belongsTo(Country::class);
   }
   public function doctors() {
      return $this->hasMany(Doctor::class);
   }
}

class Country extends Model {
   public function cities() {
      return $this->hasMany(City::class);
   }
}
一个国家可以有许多城市,但一个城市属于一个国家。一个城市可以有很多医生,但一个医生只属于一个城市

有关雄辩关系的更多信息

关于你的问题,我想这是一个GET请求,你想从一个给定的城市获得所有的医生。有很多方法可以实现这一点

您可以使用a,使用
whereHas
雄辩的方法。此方法允许根据相关表中的值筛选医生模型结果


我不想为你写下所有的代码。我鼓励您阅读有关我上面列出的工具的文档

为什么你想用get方法发送,为什么不使用post方法。请在提问之前做一些研究。您可以在laravel的de文档中找到所有内容。例如,我建议你阅读不同的方法,以及如何与之沟通。e、 g.GET用于使用查询参数查询资源,POST用于创建提供的有效负载资源,PUT用于更新资源,DELETE用于删除资源。因此,根据您的需求明智地使用每种方法。