vendredi 14 août 2020

How could i make my PHP SQL server return matches if a keyword 'search' matches somewhere in the item name?

My URL sample : say www.goodmenofearth.com

Here's the list of values inside my SQL table (products) :

id    name                description        comments
1    orange            fresh from farm     some comments
2    organic powder      easy buy now     easy comments here
3    lemon juice          easy lemon        lemon is nice
4    bread butter          quick food          quickies

Here's my route :

Route::resource('products', 'API\ProductAPIController');

Here's my index of ProductAPIController:

    public function index(Request $request)
    {
        try{
            $this->productRepository->pushCriteria(new RequestCriteria($request));
            $this->productRepository->pushCriteria(new LimitOffsetCriteria($request));
            $this->productRepository->pushCriteria(new ProductsOfFieldsCriteria($request));
            if($request->get('trending',null) == 'week'){
                $this->productRepository->pushCriteria(new TrendingWeekCriteria($request));
            }
            else{
                $this->productRepository->pushCriteria(new NearCriteria($request));
            }

            $products = $this->productRepository->all();

        } catch (RepositoryException $e) {
            return $this->sendError($e->getMessage());
        }

        return $this->sendResponse($products->toArray(), 'Products retrieved successfully');
    }

My productRepository :

application/x-httpd-php ProductRepository.php ( C++ source, ASCII text )
<?php

namespace App\Repositories;

use App\Models\Product;
use InfyOm\Generator\Common\BaseRepository;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;

class ProductRepository extends BaseRepository implements CacheableInterface
{

    use CacheableRepository;

    protected $fieldSearchable = [
        'name',
        'price',
        'discount_price',
        'description',
        'capacity',
        'package_items_count',
        'unit',
        'itemsAvailable',
        'featured',
        'store_id',
        'category_id',
        'brand_id'
    ];

    public function model()
    {
        return Product::class;
    }


    public function myProducts()
    {
        return Product::join("user_stores", "user_stores.store_id", "=", "products.store_id")
            ->where('user_stores.user_id', auth()->id())->get();
    }
}

When i do a search https://www.goodmenofearth.com/products?search=name%3Aorganic+powder here's how it is now :

Expectation :"

Return only row #2 from the table since the "name" column has 'organic' & 'powder' within the name.

Current result

Only returning table #2 as expected.

Now when i do a search https://www.goodmenofearth.com/products?search=name%3Apowder here's how it is now :

Expectation :"

Return only row #2 from the table since the "name" has powder within the name.

Current result

I don't get any results.

Can someone please help me with the changes that i must make in order to get the matching results ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire