Back to SiteSearch Support

angelistacindy51323
angelistacindy51323

I get one issue in plugins code, when i used 'thumb', Call to a member function first() on string, how can i fix it?

Last updated

OFFLINE
OFFLINE

Without any more info on where you experience this error (Line of file, with stack strace) I cannot help you.

angelistacindy51323
angelistacindy51323

when i search a word, i want to show the search result with image, but with this code i get error :

Call to a member function first() on string.

public function boot()

    {
        \Event::listen('offline.sitesearch.query', function ($query) {
            $items = Models\EMagazine::where('title', 'like', "%${query}%")
                                            ->get();
            $results = $items->map(function ($item) use ($query) {
                $relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;

                return [
                    'title'     => $item->title,
                    'url'       => '/emagazines/' . $item->slug,
                    'thumb'     => $item->img->first(),
                    'relevance' => $relevance,
                ];
            });

            return [
                'provider' => 'EMagazine',
                'results'  => $results,
            ];
        });
    }

Last updated

OFFLINE
OFFLINE

This is not a problem with the plugin but with the way you have set up your model. It looks like you did not specifiy a attachOne relationship on your model correctly:

https://octobercms.com/docs/database/attachments#file-attachments

$item->img should be a File relationship, not a string.

Dione
Dione

OFFLINE said:

This is not a problem with the plugin but with the way you have set up your model. It looks like you did not specifiy a attachOne relationship on your model correctly:

https://octobercms.com/docs/database/attachments#file-attachments

$item->img should be a File relationship, not a string.

I have a similar issue, it doesn't show any error but the image is not appearing in search result, my attachOne relationship is set as described in the documentation

'thumbnail' => 'System\Models\File'

and my Plugin.php as well

'thumb' => $item->thumbnail->first(),

sorry i don't know how to share code here

1-5 of 5