Check if Relationship Method Exists in Laravel

Answers 1
  • Eloquent relationship method exists or not in laravel

    $user = User::where('id', 1)->first();
    if (method_exists($user, 'roles')) {
        // Do something with $user->roles()->

    If your eloquent relationship method names are dynamic and you need to check if Relationship method with such name exists on the object, use method_exists($object, 'method_name') function. It returns true if the method with this particular name exists else return false.

  • Back to code snippet queries related laravel