Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve exception for search on json columns on a relationship #15139

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion packages/tables/src/Columns/Concerns/InteractsWithTableQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,52 @@ public function applySearchConstraint(EloquentBuilder $query, string $search, bo
"%{$nonTranslatableSearch}%",
),
function (EloquentBuilder $query) use ($databaseConnection, $isSearchForcedCaseInsensitive, $nonTranslatableSearch, $searchColumn, $whereClause): EloquentBuilder {
// Treat the missing "relationship" as a JSON column if dot notation is used in the column name.
if (filled($relationshipName = $this->getRelationshipName())) {
// Check if it is a JSON column on a relationship
if (str($relationshipName)->contains('.')) {
$record = $query->getModel();
$parts = explode('.', $relationshipName);

$relationshipNameParts = [];
while (count($parts) > 0) {
// Grab the first name element to check
$nestedRelationshipName = array_shift($parts);

if (! $record->isRelation($nestedRelationshipName)) {
// Not a relation, assume that all next dots represent json accessors
array_unshift($parts, $nestedRelationshipName);

break;
}

// Append the name element as relation
$relationshipNameParts[] = $nestedRelationshipName;

/** @var Relation */
$relationship = $record->{$nestedRelationshipName}();
$record = $relationship->getRelated();
}

if (filled($relationshipNameParts)) {
// There is a relationship before JSON column using dot notation
if (filled($parts)) {
// When there are remaining part(s), assume it is a JSON column
$searchColumn = implode('->', [
...$parts,
$searchColumn,
]);
}

return $query->{"{$whereClause}Relation"}(
implode('.', $relationshipNameParts),
generate_search_column_expression($searchColumn, $isSearchForcedCaseInsensitive, $databaseConnection),
'like',
"%{$nonTranslatableSearch}%",
);
}
}

// Treat the missing "relationship" as a JSON column if dot notation is used in the column name.
$searchColumn = (string) str($relationshipName)
->append('.')
->append($searchColumn)
Expand Down
Loading