laravel attach many code example
Example 1: how to use where relationship laravel
Event::with(["owner", "participants" => function($q) use($someId){
$q->where('participants.IdUser', '=', 1);
}])
Example 2: laravel detach
$user->roles()->detach($roleId);
$user->roles()->detach();
Example 3: laravel eloquent associate
$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
Example 4: laravel attach
$user->reasons->attach($reasonId);
$user->reasons->attach($reasonIds);
$user->save();
Example 5: laravel pivot table model
public function customer(){
return $this->belongsToMany('customer')->withPivot(
'start_date',
'stop_date',
'rem_date',
'due_date',
'status'
);
}
public function services(){
return $this->belongsToMany('Service')->withPivot(
'start_date',
'stop_date',
'rem_date',
'due_date',
'status'
);
}
public function staff(){
return $this->belongsToMany('Staff');
}
public function custservs(){
return $this->belongsToMany('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();
});
Example 6: latavel attach method
$user->roles()->attach($roleId, ['expires' => $expires]);