vendor and env first commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\QueryBuilder\Includes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* @template TModelClass of \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
interface IncludeInterface
|
||||
{
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder<TModelClass> $query
|
||||
* @param string $include
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __invoke(Builder $query, string $include);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\QueryBuilder\Includes;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class IncludedCallback implements IncludeInterface
|
||||
{
|
||||
protected Closure $callback;
|
||||
|
||||
public function __construct(Closure $callback)
|
||||
{
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
public function __invoke(Builder $query, string $relation)
|
||||
{
|
||||
$query->with([
|
||||
$relation => $this->callback,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\QueryBuilder\Includes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class IncludedCount implements IncludeInterface
|
||||
{
|
||||
public function __invoke(Builder $query, string $count)
|
||||
{
|
||||
$query->withCount(Str::before($count, config('query-builder.count_suffix', 'Count')));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\QueryBuilder\Includes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class IncludedExists implements IncludeInterface
|
||||
{
|
||||
public function __invoke(Builder $query, string $exists)
|
||||
{
|
||||
$exists = Str::before($exists, config('query-builder.exists_suffix', 'Exists'));
|
||||
|
||||
$query
|
||||
->withExists($exists)
|
||||
->withCasts([
|
||||
"{$exists}_exists" => 'boolean',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\QueryBuilder\Includes;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class IncludedRelationship implements IncludeInterface
|
||||
{
|
||||
/** @var Closure|null */
|
||||
public $getRequestedFieldsForRelatedTable;
|
||||
|
||||
public function __invoke(Builder $query, string $relationship)
|
||||
{
|
||||
$relatedTables = collect(explode('.', $relationship));
|
||||
|
||||
$withs = $relatedTables
|
||||
->mapWithKeys(function ($table, $key) use ($query, $relatedTables) {
|
||||
$fullRelationName = $relatedTables->slice(0, $key + 1)->implode('.');
|
||||
|
||||
if ($this->getRequestedFieldsForRelatedTable) {
|
||||
$fields = ($this->getRequestedFieldsForRelatedTable)($fullRelationName);
|
||||
}
|
||||
|
||||
if (empty($fields)) {
|
||||
return [$fullRelationName];
|
||||
}
|
||||
|
||||
return [$fullRelationName => function ($query) use ($fields) {
|
||||
$query->select($fields);
|
||||
}];
|
||||
})
|
||||
->toArray();
|
||||
|
||||
$query->with($withs);
|
||||
}
|
||||
|
||||
public static function getIndividualRelationshipPathsFromInclude(string $include): Collection
|
||||
{
|
||||
return collect(explode('.', $include))
|
||||
->reduce(function (Collection $includes, string $relationship) {
|
||||
if ($includes->isEmpty()) {
|
||||
return $includes->push($relationship);
|
||||
}
|
||||
|
||||
return $includes->push("{$includes->last()}.{$relationship}");
|
||||
}, collect());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user