for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spatie\QueryBuilder\Includes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class IncludedRelationship implements IncludeInterface
{
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('.');
$fields = $query->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());