laravel wherein on many to many relation code example
Example 1: many to many relationship laravel
use App\Models\User;
$user = User::find(1);
$user->roles()->attach($roleId);
Example 2: laravel relation has many
// Post model
public function comments()
{
return $this->hasMany(Comment::class);
}
// Post controller
$comments = Post::find(1)->comments;