HELP getGlobalSearchResults
Why if i use getGlobalSearchResults i have no attributes
This is the results
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$risultato = Pfc::query()
->where('codice', 'like', "%{$searchQuery}%")
->orWhereHas('pfcArticoli',
function($internalQuery) use ($searchQuery){
$internalQuery->join('articoli', 'articoli.id', '=', 'pfc_articolo.articolo_id')
->where('descrizione', 'like', "%{$searchQuery}%");
})
->get();
return $risultato;
}
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$risultato = Pfc::query()
->where('codice', 'like', "%{$searchQuery}%")
->orWhereHas('pfcArticoli',
function($internalQuery) use ($searchQuery){
$internalQuery->join('articoli', 'articoli.id', '=', 'pfc_articolo.articolo_id')
->where('descrizione', 'like', "%{$searchQuery}%");
})
->get();
return $risultato;
}
Solution:Jump to solution
Here's how I solved it
```php
public static function getGlobalSearchResultDetails(Model $record): array
{
//dd($record);...
3 Replies
I guess you forget to prepare how to display the result. Take a look here https://filamentphp.com/docs/2.x/admin/resources/global-search
No, I've already gone through all the documentation, but the result remains the same, even when using
error
App\Filament\Resources\PfcResource::getGlobalSearchResults(): Return value must be of type Illuminate\Database\Eloquent\Collection, Illuminate\Support\Collection returned
getGloballySearchableAttributes
, getGlobalSearchResultDetails
.
This
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$risultato = Pfc::with('articoli')
->where('codice', 'like', "%{$searchQuery}%")
->orWhereHas('pfcArticoli',
function($internalQuery) use ($searchQuery){
$internalQuery->join('articoli', 'articoli.id', '=', 'pfc_articolo.articolo_id')
->where('descrizione', 'like', "%{$searchQuery}%");
})
->get()
->map(function (Model $record): ?GlobalSearchResult {
$url = static::getGlobalSearchResultUrl($record);
if (blank($url)) {
return null;
}
return new GlobalSearchResult(
title: static::getGlobalSearchResultTitle($record),
url: $url,
details: static::getGlobalSearchResultDetails($record),
actions: static::getGlobalSearchResultActions($record),
);
})
->filter();
// dd($risultato);
// dd(parent::getGlobalSearchResults($searchQuery));
return $risultato;
}
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$risultato = Pfc::with('articoli')
->where('codice', 'like', "%{$searchQuery}%")
->orWhereHas('pfcArticoli',
function($internalQuery) use ($searchQuery){
$internalQuery->join('articoli', 'articoli.id', '=', 'pfc_articolo.articolo_id')
->where('descrizione', 'like', "%{$searchQuery}%");
})
->get()
->map(function (Model $record): ?GlobalSearchResult {
$url = static::getGlobalSearchResultUrl($record);
if (blank($url)) {
return null;
}
return new GlobalSearchResult(
title: static::getGlobalSearchResultTitle($record),
url: $url,
details: static::getGlobalSearchResultDetails($record),
actions: static::getGlobalSearchResultActions($record),
);
})
->filter();
// dd($risultato);
// dd(parent::getGlobalSearchResults($searchQuery));
return $risultato;
}
Solution
Here's how I solved it
public static function getGlobalSearchResultDetails(Model $record): array
{
//dd($record);
return [
// 'Codice' => $record->codice,
'Descrizione' => $record->articoli->pluck('descrizione')->join(', '),
];
}
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$risultato = Pfc::with('articoli')
->where('codice', 'like', "%{$searchQuery}%")
->orWhereHas('pfcArticoli',
function($internalQuery) use ($searchQuery){
$internalQuery->join('articoli', 'articoli.id', '=', 'pfc_articolo.articolo_id')
->where('descrizione', 'like', "%{$searchQuery}%");
})
->get()
->map(function (Model $record): ?GlobalSearchResult {
$url = static::getGlobalSearchResultUrl($record);
if (blank($url)) {
return null;
}
return new GlobalSearchResult(
title: static::getGlobalSearchResultTitle($record),
url: $url,
details: static::getGlobalSearchResultDetails($record),
actions: static::getGlobalSearchResultActions($record),
);
})
->filter();
return new \Illuminate\Database\Eloquent\Collection($risultato);
}
public static function getGlobalSearchResultDetails(Model $record): array
{
//dd($record);
return [
// 'Codice' => $record->codice,
'Descrizione' => $record->articoli->pluck('descrizione')->join(', '),
];
}
public static function getGlobalSearchResults(string $searchQuery): Collection
{
$risultato = Pfc::with('articoli')
->where('codice', 'like', "%{$searchQuery}%")
->orWhereHas('pfcArticoli',
function($internalQuery) use ($searchQuery){
$internalQuery->join('articoli', 'articoli.id', '=', 'pfc_articolo.articolo_id')
->where('descrizione', 'like', "%{$searchQuery}%");
})
->get()
->map(function (Model $record): ?GlobalSearchResult {
$url = static::getGlobalSearchResultUrl($record);
if (blank($url)) {
return null;
}
return new GlobalSearchResult(
title: static::getGlobalSearchResultTitle($record),
url: $url,
details: static::getGlobalSearchResultDetails($record),
actions: static::getGlobalSearchResultActions($record),
);
})
->filter();
return new \Illuminate\Database\Eloquent\Collection($risultato);
}