“Laravel mendapatkan data dalam tabel pivot” Kode Jawaban

model tabel pivot laravel

/*Model - Service*/
public function customer(){
    return $this->belongsToMany('customer')->withPivot(
        'start_date',
        'stop_date',
        'rem_date',
        'due_date',
        'status'
        );
}

/*Model - customer*/
public function services(){
    return $this->belongsToMany('Service')->withPivot(
        'start_date',
        'stop_date',
        'rem_date',
        'due_date',
        'status'
        );
}

////These following relations didnt workout
/*Model - custserv*/ //uses the pivot table customer_service//
public function staff(){
    return $this->belongsToMany('Staff');
}

/*Model - Staff*/
public function custservs(){
    return $this->belongsToMany('Custserv');
}

/*schema for pivot table 'staff' and 'Custserv' */
Schema::create('customer_service_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_service_id')->unsigned()->index();
        $table->foreign('customer_service_id')->references('id')->on('customer_service')->onDelete('cascade');
        $table->integer('staff_id')->unsigned()->index();
        $table->foreign('staff_id')->references('id')->on('staff')->onDelete('cascade');
        $table->timestamps();
    });
Hurt Hoopoe

Laravel mendapatkan data dalam tabel pivot

// In model User.php, add withPivot; for ex :
public function customer(){
    return $this->belongsToMany('role')
                ->withPivot('type'); // 'type' is from pivot table user_role
}

// then access the field with ->pivot; for ex:
$current_user->customer->pivot->type
Comfortable Cheetah

Jawaban yang mirip dengan “Laravel mendapatkan data dalam tabel pivot”

Pertanyaan yang mirip dengan “Laravel mendapatkan data dalam tabel pivot”

Lebih banyak jawaban terkait untuk “Laravel mendapatkan data dalam tabel pivot” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya