在范围内设置后重写列

I wrote a scope which restricts all queries on a model. Those results should contain only data, which can be seen by the logged in user. To restrict such a query I join to a user table and limit the results by id IN (?,?,?,?,?,?,?,?). Further in that scope I use select([BASETABLE . '.*']) because I don't want the data of the partner join. Until here everything works fine. If I use that model and change the columns to what I need in that case, the columns keeps the definition from my scope. I can't overwrite them later.

Anyone has a solution or an idea?

I already looked into the classes (MysqlProcessor, MysqlConnection, Model, Eloquent\Builder, Query\Builder, ...) of laravel but can't find a solution.

// in scope called method to restrict
public function restrictByIds (Builder $query, Model $model)
{
    $ids = $this->getIdsByUser(); // [1,2,3,4,5,6]
    if (!empty($ids)) {
        $userModel = $model->user()->getModel();
        $userTable = $userModel->getTable();

        $query
            ->getQuery()
            ->select([$model->getTable() . '.*'])
            ->join(
                $userTable,
                $userTable . '.id',
                '=',
                $model->getTable() . '.user_id'
            )
            ->whereIn($userTable . '.id', $ids);
    }
}




// tries to overwrite columns
$model->getQuery()->select(['DATE(created) as time', 'count(*) AS count']);
$model->getQuery()->columns = ['DATE(created) as time', 'count(*) AS 
count'];
$model->getQuery()->addSelect(['DATE(created) as time', 'count(*) AS 
count']);
$model->getQuery()->addBinding(['DATE(created) as time', 'count(*) AS 
count'], 'select');
$model->getQuery()->bindings = ['select' => ['DATE(created) as time',
'count(*) AS count']];
$model->select(\DB::raw('DATE(created) as time, count(*) AS count'));
$model->columns = ['DATE(created) as time', 'count(*) AS count'];
$model->addSelect(\DB::raw('DATE(created) as time, count(*) AS count'));
$model->addBinding(['DATE(created) as time', 'count(*) AS count'], 
'select');
$model->bindings = ['select' => ['DATE(created) as time', 'count(*) AS 
count']];

I found a solution. $model = Model::query() returns the Eloquent\Builder with active scope. To define the columns I use $model->getQuery()->select(). For any other functions (where, whereIn, orderBy, groupBy, ...) $model itself can be used. This way i get my custom columns with automated restriction.

And use $model->getQuery()->addSelect in scopes instead of $model->getQuery()->select.