xxxxxxxxxx
// inside Users model
public function roles(){
return $this->belongsToMany(Role::class, 'user_roles')
->withPivot('department_id');
}
/*
the example is a pivot table named user_roles and it has these fields:
user_id, role_id, department_id
*/
xxxxxxxxxx
// 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
xxxxxxxxxx
$order = Order::query()->withSum('products', 'order_product.quantity')->get();
xxxxxxxxxx
// A user has many roles
// The BelongsToMany relationship uses a pivot table
public function roles(): BelongsToMany {
return $this->belongsToMany(
User::class,
'users_roles',
'user_id',
'role_id'
);
}
// Example roles
// Role 1 = 'Editor';
// Role 2 = 'Guest';
// Lets update the attached roles for each user using sync()
User::chunk(5000, function ($users) { // Get a chunk of the users
$users->each(function ($user) { // Loop through the chunk of users
$user->roles()->sync([1, 2]); // Sync the users roles to be ONLY 1 & 2 (Sync will remove all other attached roles)
});
});
// The pivot table is now updated // No need to manually update the pivot table
xxxxxxxxxx
/*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();
});
xxxxxxxxxx
public function users()
{
return $this->belongsToMany(User::class)->withPivot('role')->withTimestamps();
}
public function houses()
{
return $this->belongsToMany(House::class)->withPivot('role')->withTimestamps();
}
$house = auth()->user()->houses()->create(
[
"name" => $request->name,
"description" => $request->description,
],
[
"role" => "owner"
]
);
xxxxxxxxxx
return new class extends Migration {
public function up()
{
Schema::create('blog_category', function (Blueprint $table) {
$table->foreignIdFor(Blog::class)->constrained()->onDelete('cascade');
$table->foreignIdFor(Category::class)->constrained()->onDelete('cascade');
$table->primary(['blog_id', 'category_id']);
$table->index('blog_id');
$table->index('category_id');
});
}