1

I have a Controller and I need to set the Database for my Query Builder, all is working but when I create new function I need to redeclare a connection, What I need is to declare the connection so that the whole controller will be connecting with that database.

class CompanyInformationController extends Controller
{



    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function firstFunction()
    {

     $connection = DB::connection('fdis_1');

      return  $connection->getDatabaseName();

    }

    public function secondFunction()
    {
     // This is redundant
     $connection = DB::connection('fdis_1');

      return  $connection->getDatabaseName();

    }
}
3
  • 2
    Declare connection in constructure and use into whole controller Commented Apr 3, 2018 at 8:42
  • @RavindraBhanderi do you have example ? Commented Apr 3, 2018 at 8:43
  • ya i write ans for you Commented Apr 3, 2018 at 8:44

1 Answer 1

4

in a class on controller

 private $connection;

 public function __construct()
    {
        $this->connection = DB::connection('fdis_1');

    }

now use into your method like

  $this->connection->getDatabaseName();
Sign up to request clarification or add additional context in comments.

1 Comment

@MartneyAcha Welcome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.