Codeguru
Codeguru

Reputation: 31

How do I send parameters to Laravel?

I have doctors and doctor_country_city table.

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');
    });

I would like to send multiple parameters to the route. What should my controller file and model relationship be like?

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

Upvotes: 0

Views: 297

Answers (1)

xyzale
xyzale

Reputation: 795

Before giving you the solution I'd suggest you to rethink your database design.

Can a doctor belong to many cities? If not you don't need the doctor_country_city pivot table in the first place. Also you shouldn't relate the doctor to a city and a country as well. That's quite weird because your database potentially allows to assign a doctor to New York, France. A city by definition belongs to one country only.

So I'd rather relate a doctor to a city which is related to a country.

Dr. John Doe > New York > USA

That makes more sense to me. So you'll have one more table called countries and their models will have relationships like so:

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);
   }
}

One country can have many cities, but one city belongs to one country. One city can have many doctors, but a doctor belongs to one city only.

For further information about eloquent relationships check the documentation.

Going to your question, I suppose that's a GET request where you want to get all doctors from a given city. There are many ways to achieve that.

You can use a scope, using the whereHas Eloquent method. This method allows to filter your Doctor model results by a value in a related table.

I don't want to write down all the code for you. I encourage you to read the documentation about the tools I listed above.

Upvotes: 1

Related Questions