Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    laravel whereHas with multiple database connections

    Laravel Framework
    laravel
    2
    3
    811
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Nubelle
      Nubelle Web Development last edited by

      I have small issue with Laravel application , currently I am using two database connection and I want to change the connection in whereHas method.

      here is my code .

      //Relation Ship Methods
      public function technician(){
              return $this->setConnection('common_database')->belongsTo('App\User', 'technician_id');
          }
      
          public function commitee(){
              return $this->setConnection('common_database')->belongsTo('App\User', 'commitee_validator_id');
          }
      
      //controller functions 
      $practices->whereHas(‘technician’, function($q) use($search) {
       $q->where(‘first_name’, 'like', '%' . $search . '%');
      });
      
      1 Reply Last reply Reply Quote 1
      • root
        root Linux Help last edited by root

        In whereHas you cannot change the database connection, you have to change connection before where has like this

        //get users ids with where
        $user_ids = User::where('first_name', 'like', '%' . $search . '%')->pluck('id');
        

        In User modal you must specify the database connection before above where statement

         protected $connection = 'common_database';
        

        Then where in function to find the practices with user ids

         $practices->whereIn('technician_id', $user_ids); 
        
        Nubelle 1 Reply Last reply Reply Quote 1
        • Nubelle
          Nubelle Web Development @root last edited by

          @root thanks root . let me check it

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post

          1
          Online

          3.6k
          Users

          1.3k
          Topics

          5.3k
          Posts

          • Privacy
          • Terms & Conditions
          • Donate

          © Copyrights and All right reserved Lanka Developers Community

          Powered by Axis Technologies (PVT) Ltd

          Made with in Sri Lanka

          | |