attach eloquent 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 how to query belongsTo relationship
$movies = Movie::whereHas('director', function($q) {
$q->where('name', 'great');
})->get();