I
I
Ivan Vovchok2020-01-28 12:39:35
Laravel
Ivan Vovchok, 2020-01-28 12:39:35

How to dynamically change setConnection() for a model in Laravel?

I have a controller with the following code:

private function unassignedLinks(Request $request)
    {
        return Link::whereNull('assignee_id')
            ->whereIn('status', [
                Link::LINK_STATUS_APPROVED,
                Link::LINK_STATUS_POST_ANSWER
            ])
            ->with('forum', 'order', 'question_account', 'answer_account')
            ->orderCorrectly()
            ->paginate(10, ['*'], 'uap')
            ->appends($request->except('uap'));
    }

Is it possible somehow to establish a dynamic connection to different databases through this function before returning setConnection(), so that data is returned from the database specified in the connection?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2020-01-28
@JhaoDa

Feel free to study the framework code , take two.

N
NubasLol, 2020-01-28
@NubasLol

Link::resolveConnection($connection = null)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question