Multisite and accessing models that use Multisite Trait

Hi there,

I am using multisite on a custom plugin and have added the trait to the model.

    use \October\Rain\Database\Traits\Multisite;
    protected $propagatable = [];

All good there and the backend and frontend is working as expected.

However, if I try to use a route and make an endpoint for an API that returns the records, I can only access the primary site records:

Route::get('/myendpoint', function () {
        $data = MyModel::all();
        return $data;
    });

Only returns primary site records.

I tried

Route::get('/myendpoint', function () {
        $data = MyModel::where('site_id','=','[alt site ID]')->get();
        return $data;
    });

and also

Route::get('/[siteprefix]/myendpoint', function () {
        $data = MyModel::all();
        return $data;
    });

but no luck.

I also tried this in the php section of a CMS page


function onStart(){
    $founddata = MyModel::where('site_id','=', '[primary site-ID]')->get();
    $nodatahere = MyModel::where('site_id','=', '[alt-site-ID]')->get();
}

And as expected if I changed the site URL to the alt site on the frontend I would get the alt-site data with this, but then lose all the primary site data.

Is there a way to query the site-specific records from a model. All I am trying to do is to copy some data from one site to another, so I need to access the two sets of records to do this.

Thanks

Hi @roojai

Try adding this middleware to the route:

->middleware(\System\Middleware\ActiveSite)

Hopefully it helps.

1 Like

@daft

Yes, that helps a lot, thanks!

But you become a model translated, but all relations are empty.

:frowning:

What is the right way to get activeSiteId in backend and frontend ?

Found this regarding the site_id, seems correct:

// for backend
$site = Site::getEditSite();

// for frontend
$site = Site::getActiveSite();