Closed vheins closed 1 year ago
Can you provide a test repository that recreates this issue, or write a failing test in a PR that recreates this issue?
This is my controller,
if ($request->input('search_type')) $this->search_type = Str::snake($request->search_type);
if ($request->input('per_page')) $this->per_page = $request->per_page;
if ($request->input('sort_by')) $this->sort_by = Str::snake($request->sort_by);
if ($request->input('sort_key')) $this->sort_key = ($request->sort_key == 'asc') ? 'asc' : 'desc';
if ($request->input('search')) {
$search = Str::lower(str_replace(' ', '%', $request->input('search')));
if (Str::contains($this->search_type, '.')) {
$keys = explode('.', $this->search_type);
foreach ($keys as $value) {
$key[] = Str::camel($value);
}
$searchKey = end($key);
array_pop($key);
$relation = implode('.', $key);
$data->whereHas($relation, function ($q) use ($search, $searchKey) {
$table = $q->getModel()->getTable();
$q->where($table . '.' . Str::snake($searchKey), 'like', '%' . $search . '%');
});
} else {
$searchType = Str::of($this->search_type)->camel()->toString();
if (isset($this->mapRelation[$searchType])) {
$search_type = $this->mapRelation[$searchType];
} else {
$search_type = $this->search_type;
}
$case = is_numeric($search) ? $search_type : "LOWER(" . $search_type . ")";
$data->whereRaw($case . " like '%" . $search . "%'");
}
}
its work before on laravel 9.53 an this version 0.12.5
but when i try to upgrade to Laravel 10 & need upgrade laravel-model-caching to latest version, its throw an error Object of class Illuminate\Database\Query\Expression could not be converted to string so i'm revert back to laravel 9.53 and 0.125
its same to
$data->whereHas('user', function ($q) use ($search, $searchKey) {
$table = $q->getModel()->getTable();
$q->where('users.name', 'like', '%boo yaaa%');
});
or
$data->whereHas('other.relation', function ($q) use ($search, $searchKey) {
$table = $q->getModel()->getTable();
$q->where('relation.field, 'like', '%boo yaaa%');
});
i Found the issue , its on getColumnClauses
protected function getColumnClauses(array $where) : string
{
if ($where["type"] !== "Column") {
return "";
}
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
}
i try to dump $where variable and got
array:5 [ // vendor/genealabs/laravel-model-caching/src/CacheKey.php:85
"type" => "Column"
"first" => Illuminate\Database\Query\Expression {#2342
#value: ""subscriptions"."subscriber_id""
}
"operator" => "="
"second" => "users.id"
"boolean" => "and"
]
in my case $where["first"] return Illuminate\Database\Query\Expression refer to https://laravel.com/docs/10.x/upgrade#database-expressions however, if your application is manually casting database expressions to strings using (string) or invoking the __toString method on the expression directly, you should update your code to invoke the getValue method instead:
so i try overide that variable into
$where["first"] = $where["first"]->getValue(\DB::connection()->getQueryGrammar());
and its works
may i send pull request @mikebronner ?
i had send PR on #442 Thanks you
Fix at #442
Describe the bug A clear and concise description of what the bug is.
Eloquent Query Please provide the complete eloquent query that caused the bug, for example:
Stack Trace
Environment
Additional context Add any other context about the problem here.