samedi 11 novembre 2017

Value of Id is not entering in pivot table

I am working on many to many relation in laravel. I have made a pivot table and I am entering records but problem is coming with that my doctor id is not being entering in my pivot table. I am sharing my code below of my tables and controller.

Code of controller is:

  if($request->hospitalId != null)
    {
        $IdArray = explode(',', $request->hospitalId);
      // $doc= DB::table('doctors')->where('doctorId', $request->doctorId)->first();
        $doc = Doctor::where('id', $request->id)->first();
        foreach ($IdArray as $Id) {
            $doc->hospitals()->attach($request->hospitalId);
        }
    }

Code of my doctor migration

public function up()
{
    Schema::create('doctors', function (Blueprint $table) {
        $table->string('id');
        $table->string('name');
        $table->string('gender');
        $table->string('email');
        $table->string('picture')->default('default.jpg');
        $table->string('specifications');
        $table->string('description');
        $table->integer('receptionistId')->unsigned();
        $table->timestamps();
    });
}

Code of my pivot table

*/
public function up()
{
    Schema::create('doctor_hospital', function (Blueprint $table) {
        $table->increments('id');
        $table->string('doctor_id');
        $table->foreign('doctor_id')->references('id')->on('doctors')->onDelete('cascade');
        $table->string('hospital_id');
        $table->foreign('hospital_id')->references('hospitalId')->on('hospitals')->onDelete('cascade');
        $table->timestamps();
    });
}

Please tell me what I have done wrong as I want to enter the record of id which user enters but its not entering that id instead its showing me 0.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2yRqIVV
via IFTTT

Aucun commentaire:

Enregistrer un commentaire