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

multiple images problem #56

Closed
amjadniazi48 opened this issue Apr 28, 2018 · 2 comments
Closed

multiple images problem #56

amjadniazi48 opened this issue Apr 28, 2018 · 2 comments

Comments

@amjadniazi48
Copy link

amjadniazi48 commented Apr 28, 2018

i m retrieving attachMany relationship postingimage in this way but when i print {{result.model}} in content.htm file it shows null array in posting images
{"title":"gdsag","brandname":"Yamaha","description":"gdsga","price":5887,"name":"Rawalpindi","postingimage":[]}
cannot able to get the saved images in model


public function boot()
    {

        \Event::listen('offline.sitesearch.query', function ($query) {

            // Search your plugin's contents

            $city = get('cname');
            $brandname = get('brandname');
            $minprice = get('minprice');
            $maxprice = get('maxprice');
            if ($minprice && $maxprice) {
                $price = true;
            } else {
                $price = false;
            }

            $items = Models\Posting::join('fsz_posting_tblbrands', 'fsz_posting_tblposting.brand_id', '=', 'fsz_posting_tblbrands.id')

                ->join('vojtasvoboda_locationtown_towns', 'fsz_posting_tblposting.city_id', '=', 'vojtasvoboda_locationtown_towns.id')
                ->select('title', 'brandname', 'fsz_posting_tblposting.description', 'price', 'name')
                ->where('brandname', 'like', "%${query}%")
                ->when($city, function ($items) use ($city) {
                    return $items->where('vojtasvoboda_locationtown_towns.name', 'like', "%${city}%");
                })
                ->when($price, function ($items) use ($minprice, $maxprice) {
                    return $items->whereBetween('price', ["${minprice}", "${maxprice}"]);
                }) ->get();
            // Now build a results array
            $results = $items->map(function ($item) use ($query) {
                // If the query is found in the title, set a relevance of 2
                $relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;
                if ($item->postingimage) {
                    return [
                        'title' => $item->title,
                        'text' => $item->description,
                        'text' => $item->price,
                        'text' => $item->brandname,
                        'text' => $item->name,
                        'thumb' => $item->postingimage->first(),
                   
                        'relevance' => $relevance, // higher relevance results in a higher
                        'model' => $item, 
                    ];
                } else {
                    return [
                        'title' => $item->title,
                        'text' => $item->description,
                        'text' => $item->price,
                        'text' => $item->brandname,
                        'text' => $item->name,
                        'model' => $item,
                    ];

                }
            });

            return [
                'provider' => 'Postings', // The badge to display for this result
                'results' => $results,
            ];
        });
    }

my init method in component

 public function init()
    {
        $component = $this->addComponent(
            'Responsiv\Uploader\Components\FileUploader',
            'fileUploader',
            ['deferredBinding' => true]
        );


        $component->bindModel('postingimage', new Posting);
      
     
    }
@tobias-kuendig
Copy link
Member

tobias-kuendig commented Apr 30, 2018

Check in your boot method if the images are loaded. This does not sound like a problem caused by this plugin.

$results = $items->map(function ($item) use ($query) {
    dd($item->postingimage);
});

If this returns an empty result the problem has nothing to do with sitesearch.

Also make sure that your $item is actually an instance of your model and not only a Collection. I'm not sure if you'll get a model instance returned if you use joins. If it is a simple Collection you won't be able to load the relations off of it. This would be a simple workaround to this problem:

$matching = Posting::join('fsz_posting_tblbrands') // ... do your join

// Now refetch the needed models with all needed relations
$items = Posting::with('postingimage')->whereIn('id', $matching->pluck('id'))->get();

Also, be aware that you can only return one text value in your results array.

@amjadniazi48
Copy link
Author

thanks a lot,your suggestion is works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants