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

amir tavakolian's avatar

stancl/tenancy, insert data in tenant database

hi im using stancl/tenancy for having multi tenancy.

when i insert data in database, the data goes to central database not in tenant database.

how can i insert data into tenant database?

0 likes
9 replies
Sinnbeck's avatar

@amir tavakolian ok maybe show the code that does it in the wrong database? Might be faster than us asking for each step in the documentation :)

Are you sure you are in the context of a tenent when making the query?

amir tavakolian's avatar

@Sinnbeck my code is just a simple create method. my question is why it does not insert in tenant database. i have done what ever is said in document

    $form = Form::updateOrCreate(
        [
            'id' => $this->selected_form['id'] ?? ''
        ],
        [
            'author_id'     => Auth::id(),
            'type'          => "admin"
            'title'         => $this->title,
            'content'       => $this->content,
        ]
    );

}

amir tavakolian's avatar

@Sinnbeck yes thats ok. when i use dd(Form::all()) it sends request to tenant database and thats ok. but when i want to get data without dd() i do not know why it sends request to central domain database

i want to define a seprate database connection for each tenant but i do not know how to get the tenant database name. i used tenancy() helper but it returns null

Please or to participate in this conversation.