dimanche 17 mai 2020

How to make Laravel eloquent request with 1 filter on many fields

In my Laravel 5.7/ mysql app I make request on a form with 10 filter inputs, one of them ($filter_search) if non empty must be compared with all fields(string, number, date, ref to fields of other tables) in resulting listing.

I made scope on this table fields:

    public function scopeGetBySearch($query, $search = null)
    {
        if (empty($search)) {
            return $query;
        }
        $tb= with(new StorageSpace)->getTable();
        return $query->where(
            $tb.'.number',  $search
        ) ->orWhere(function ($query) use ($search, $tb) {
            $query->where( $tb.".notes",  'like',  '%'.$search.'%' )
                ->orWhere($tb.".selling_range", $search)
                    ->orWhere($tb.".actual_storage_rent", $search)
                        ->orWhere($tb.".insurance_vat", $search)
//                            ->havingRaw("job_ref_no", $search)

        })

But I have a problem how can I set filter on job_ref_no field from other table :

 $storageSpacesCollection = StorageSpace
        ::getByStatus($filter_status)
        ->getById($relatedStorageSpacesArray)
        ->getByLocationId($filter_location_id, 'warehouses')
        ->getByCapacityCategoryId($filter_capacity_category_id, 'storage_capacities')
        ->getByLevel($filter_level)
        ->getByNumber($filter_number, true)
        ->orderBy('storage_spaces.id', 'asc')
        ->getByStorageCapacityId($filter_storage_capacity_id)
        ->getByClientId($filter_client_id)
        ->getByColorId($filter_color_id)
        ->getBySearch($filter_search)

// ->havingRaw("job_ref_no = " . $filter_search)

        ->leftJoin( 'storage_capacities', 'storage_capacities.id', '=', 'storage_spaces.storage_capacity_id' )
        ->leftJoin( 'warehouses', 'warehouses.id', '=', 'storage_spaces.warehouse_id' )
        ->leftJoin( 'clients', 'clients.id', '=', 'storage_spaces.client_id')
        ->select(
            "storage_spaces.*",

            \DB::raw( "CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) as storage_capacity_name" ),

            \DB::raw("( SELECT check_ins.job_ref_no FROM check_ins WHERE // I got job_ref_no field in subquesry  check_ins.storage_space_id=storage_spaces.id ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no"),

            "warehouses.name as warehouse_name",
            "clients.full_name as client_full_name")
        ->get();

havingRaw does not work both in the scope and in the request above if to uncomment it.

2) I tried to use addSelect, like:

But with request :

        $storageSpacesCollection = StorageSpace
            ::getByStatus($filter_status)
            ->whereRaw('storage_spaces.id <= 8') // DEBUGGING
            ->getById($relatedStorageSpacesArray)
            ->getByLocationId($filter_location_id, 'warehouses')
            ->getByCapacityCategoryId($filter_capacity_category_id, 'storage_capacities')
            ->getByLevel($filter_level)
            ->getByNumber($filter_number, true)
            ->orderBy('storage_spaces.id', 'asc')
            ->getByStorageCapacityId($filter_storage_capacity_id)
            ->getByClientId($filter_client_id)
            ->getByColorId($filter_color_id)
            ->getBySearch($filter_search)

//            ->havingRaw("job_ref_no = " . $filter_search)

            ->leftJoin( 'storage_capacities', 'storage_capacities.id', '=', 'storage_spaces.storage_capacity_id' )
            ->leftJoin( 'warehouses', 'warehouses.id', '=', 'storage_spaces.warehouse_id' )
            ->leftJoin( 'clients', 'clients.id', '=', 'storage_spaces.client_id')
            ->select(
                "storage_spaces.*",
                \DB::raw( "CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) as storage_capacity_name" ),
                "warehouses.name as warehouse_name",
                "clients.full_name as client_full_name")
            ->addSelect([
                'job_ref_no' => CheckIn::selectRaw('job_ref_no')->whereColumn('check_ins.storage_space_id', 'storage_spaces.id'),
            ])
            ->get();

But I got an error:

 local.ERROR: stripos() expects parameter 1 to be string, object given {"userId":11,"email":"nilovsergey@yahoo.com","exception":"[object] (ErrorException(code: 0): stripos() expects parameter 1 to be string, object given at /mnt/_work_sdb8/wwwroot/lar/the-box-booking/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php:1031)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'stripos() expec...', '/mnt/_work_sdb8...', 1031, Array)
#1 /mnt/_work_sdb8/wwwroot/lar/the-box-booking/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php(1031): stripos(Object(Illuminate\\Database\\Eloquent\\Builder), ' as ')
#2 [internal function]: Illuminate\\Database\\Query\\Grammars\\Grammar->wrap(Object(Illuminate\\Database\\Eloquent\\Builder))

Which way is valid ?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire