1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/Traits/Excludable.php

47 lines
1.2 KiB
PHP
Raw Normal View History

2023-01-21 06:52:24 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2023-01-21 06:52:24 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Models\Traits;
2023-04-27 00:05:57 +02:00
use Illuminate\Support\Facades\Schema;
2023-04-26 23:46:59 +02:00
/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @extends \Illuminate\Database\Eloquent\Builder<TModelClass>
* @mixin \Illuminate\Database\Eloquent\Builder
*/
2023-02-16 02:36:09 +01:00
trait Excludable
{
2023-01-21 06:52:24 +01:00
/**
* Get the array of columns
2023-02-16 02:36:09 +01:00
*
2023-01-21 06:52:24 +01:00
* @return mixed
*/
2023-02-16 02:36:09 +01:00
private function getTableColumns()
{
2023-04-27 00:05:57 +02:00
/** @var Schema|\App\Models\BaseModel $this */
2023-01-21 06:52:24 +01:00
return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}
/**
* Exclude an array of elements from the result.
2023-04-26 15:17:49 +02:00
* @param Builder $query
* @param array $columns
2023-02-16 02:36:09 +01:00
*
2023-04-27 00:05:57 +02:00
* @return Builder<BaseModel>
2023-01-21 06:52:24 +01:00
*/
2023-04-26 15:17:49 +02:00
public function scopeExclude($query, $columns): \Illuminate\Database\Eloquent\Builder
2023-01-21 06:52:24 +01:00
{
2023-04-27 00:05:57 +02:00
/** @var \Illuminate\Database\Eloquent\Builder|static $query */
2023-01-21 06:52:24 +01:00
return $query->select(array_diff($this->getTableColumns(), (array) $columns));
}
}