Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codetycon's avatar

How to make relation between 3 tables in laravel using laravel relation?

I have three tables:

SET NAMES utf8;
SET time_zone = '+00:00';

DROP TABLE IF EXISTS `api_credentials`;
CREATE TABLE `api_credentials` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL,
  `api_provider_id` bigint(20) unsigned NOT NULL,
  `access` text COLLATE utf8_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `api_credentials_user_id_foreign` (`user_id`),
  KEY `api_credentials_api_provider_id_index` (`api_provider_id`),
  CONSTRAINT `api_credentials_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `api_credentials_api_provider_id_foreign` FOREIGN KEY (`api_provider_id`) REFERENCES `api_providers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


DROP TABLE IF EXISTS `api_providers`;
CREATE TABLE `api_providers` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(255) COLLATE utf8_unicode_ci NOT NULL,
  `table_prefix` char(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


-- 2019-12-05 13:38:06

Here is my Model:

  1. ApiCredential
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class ApiCredential extends Pivot
{


    
}
  1. ApiProvider

    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class ApiProvider extends Model
    {
    
        protected $fillable = [
            'name',
            'table_prefix'
        ];
       public function users()
        {
            return $this->belongsToMany('App\Models\User')->using('App\Models\ApiCredential');
        }
    }
    
    
  2. user model

    
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    use App\Models\ApiProvider;
    use App\Models\ApiCredential;
    
    class User extends Authenticatable
    {
        use Notifiable;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name',
            'email',
            'password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password',
            'remember_token',
        ];
       
        public function apiProvider()
        {  
             return $this->belongsToMany('App\Models\ApiProvider','api_providers','user_id','api_provider_id');
        } 
    
        
         public function hasCredentials(String $apiProvider)
        {
          return $this->apiProvider()->where('name', $apiProvider)->get();
        } 
        
        
    }
    
    


**Now What I want:**
I want to get record from api_credentials table based on user and api_provider. Let say if I have three records in api_providers (names are adscane, cpalead, offertorro). I

I am running this function to get api_credential of `cpalead`.

    User::find(1)->hasCredentials('cpalead');

But it is not giving an expected result.

Can someone please help me to fix this issue.
0 likes
3 replies
Sinnbeck's avatar

But it is not giving an expected result.

So it is giving you a result? What is wrong with the result?

codetycon's avatar

It is giving tru in all conditions if the record exist is not in api_credentials

Please or to participate in this conversation.