attach laravel code example
Example 1: laravel detach
$user->roles()->detach($roleId);
$user->roles()->detach();
Example 2: whereHas site:https://laravel.com/docs/
use Illuminate\Database\Eloquent\Builder;
$posts = Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'code%');
})->get();
$posts = Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'code%');
}, '>=', 10)->get();
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: latavel attach method
$user->roles()->attach($roleId, ['expires' => $expires]);