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

Thameera's avatar

Update function in multiple database connections in same laravel app

 $db_ext = \DB::connection('mysql2');
 $countries = $db_ext->table('kotitems')->get();
        print_r($countries);die;

here i update to second database but this works fine but when i use update function' like

$db_ext->table('kotitems')->where('id',$items->name) ->update(['item_unit'=>$remain]);

it searches in database 1 and pass error message as kot.table not found but it needs to serch in scs database i can read from second database but cant write in to it,but i can't connect the table through model if it is it will be ok.

0 likes
1 reply
Thameera's avatar

I got the ans i did by using eloquent model and connect the second database through it

class Item extends Eloquent
{
    protected $connection = 'mysql2';
    use SoftDeletes;
    protected $dates = ['deleted_at'];

    protected $table = 'kotitems';
    public $fillable = ['item_code','item_name','item_unit','min_level','meassure'];

}

Please or to participate in this conversation.