inertiajs / pingcrm

A demo application to illustrate how Inertia.js works.
http://demo.inertiajs.com
MIT License
2.1k stars 767 forks source link

How to specify which database shoud a inertia::render use? #200

Closed MaciejSuchanski closed 5 months ago

MaciejSuchanski commented 1 year ago

I have 2 databases and by default a return Inertia::render takes the first one, i need to be able to pick the second one both are setup with different names in .env and config/database.php. In normal laravel return i would do: return \DB::connection('db2') ->table('clients')....

How to do it in inertia? For example on this from pingcrm: public function index() { return Inertia::render('Organizations/Index', [ 'filters' => Request::all('search', 'trashed'), 'organizations' => Auth::user()->account->organizations() ->orderBy('name') ->filter(Request::only('search', 'trashed')) ->paginate(10) ->withQueryString() ->through(fn ($organization) => [ 'id' => $organization->id, 'name' => $organization->name, 'phone' => $organization->phone, 'city' => $organization->city, 'deleted_at' => $organization->deleted_at, ]), ]); }

ntaylor-86 commented 1 year ago

Not specifically an inertia question, more of a Laravel question.

1. Setup your model

Define which connection to use in your Eloquent model.

class MyModel extends Eloquent {
    protected $connection = 'db2';
}

2. Controller

Just like in normal Laravel controller that returns a view, get your data and pass it as a parameter on the render function.

$data = MyModel::query()
        ->where('foo', 'bar')
        ->get();

return Inertia::render('Folder/View', [
    'data' => $data
]);
driesvints commented 5 months ago

Thanks for helping here @ntaylor-86