0

I am using laravel 8 and would like to query a model from my production database.

I tried the following:

DB::connection('mysql_prod')->Company::where('symbol', $symbol)
            ->first();

However, I get the following error:

Undefined property: Illuminate\Database\MySqlConnection::$Company

I would like to get a laravel model - in this case Company - back.

Any suggestions how to implement this?

2 Answers 2

2

use on if you are changing for one query.

 Company::on('mysql_prod')->where('symbol', $symbol)->first();

define $connection property in Company model so no need to mention connection

protected $connection = 'mysql_prod';

so query is

Company::where('symbol', $symbol)->first();

Ref:https://laravel.com/docs/8.x/eloquent#database-connections

Sign up to request clarification or add additional context in comments.

2 Comments

Thx for your reply! I would like to define the connection property locally as I only want to change the connection in this 1 function.
@Carol.Kar use on Company::on('mysql_prod')->where('symbol', $symbol)->first();
1

You can use

Company::on('mysql_prod')->where('symbol', $symbol)
            ->first();

Comments

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.