0

I have the following situation. I want to build some Models to use on different prior configured databases. In this case I have a model named Category which should work on a few tables on a Magento 2 database. I want to reuse that model for different databases. Here is the simplified model:

namespace App\Models\Magento;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $attributeIds = [];

    protected $table = 'catalog_category_entity';
    protected $primaryKey = 'entity_id';

    protected $fillable = ['entity_id', 'store_id', 'website_id'];
}

If I set the property $connection to my desired database, I can use the ->all() method and get a wonderful Collection of entries. But as mentioned, to use it on different databases, all the best practises show two options, the static and non-static way. Both are not working for me.

The static way (->on('MY_CONNECTION')) results in still using the default connection and not my connection. If I use the non-static way (->setConnection('MY_CONNECTION')) it also goes to the default connection. If I do a dd() on the model right before the ->all() it says $connection = 'MY_CONNECTION'. But as soon as I run it, it's the default connection again.

    $categoryAggegator = new MagentoCategory;
    $categoryAggegator->setConnection('MY_CONNECTION');

    //dd($categoryAggegator); // Here it is MY_CONNECTION

    $categories = $categoryAggegator->all();

    // Here it throws an exception, because the table is not present in the default connection

Any ideas?

Keenora Fluffball
  • 1,647
  • 2
  • 18
  • 34
  • 1
    You should use the on() method, like : User::on('dusk')->count() – Lk77 Jun 30 '23 at 13:38
  • I've tried that already, but same result :( – Keenora Fluffball Jun 30 '23 at 14:34
  • use the key on `connections` from the `config\database.php` with the driver of `MY_CONNECTION` on the `DB::connection('key')` like [docs](https://laravel.com/docs/10.x/database#using-multiple-database-connections) since the `$connections` on model is for a `string`. check if [helps](https://stackoverflow.com/questions/28985472/change-database-connection-in-laravel-model). – francisco Jun 30 '23 at 16:19
  • Does this answer your question? [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – Wojciech Czerniak Jul 01 '23 at 10:07
  • No it does not. I do have multiple connections in my database.php configured. It works as expected if I use the $connection fixed inside of the model class. – Keenora Fluffball Jul 03 '23 at 07:09

1 Answers1

3

in your config/database.php add a new array item inside "connections" array.

And now in your model file you can mention the connection name

protected $connection = 'mysql2';

If you want to change on fly then use MagentoCategory::on('mysql2')->get();

Aftab
  • 330
  • 4
  • 16
  • https://stackoverflow.com/a/55687837/4227367 use this link for better clarity. – Aftab Jun 30 '23 at 17:38
  • No this one does not help. I already have multiple working database connections configured. The model works fine, as long as I use the $connection property inside the model class. But I want to change it on the fly. It sets the $connection property when I check it, but when I want to use ->all() it throws an exception, as it uses the default, ALWAYS. – Keenora Fluffball Jul 03 '23 at 07:11
  • MagentoCategory::on('mysql2')->get(); – Aftab Jul 03 '23 at 16:02
  • Sadly the same result. As soon as I do a dd() on the MagentoCategory (with the ->on), I do get the correct database connection. If I run ->get or ->all on the builder, I do get the default database connection :( – Keenora Fluffball Jul 04 '23 at 07:18