Search code examples
phpoctobercmsoctobercms-backendoctobercms-plugins

OctoberCMS plugin extend search filter with custom query


I have created one plugin call it as "Property" and in which in listing, I am showing multiple address fields into one column which will combine multiple fields of address like postcode, street type, street number etc ..

And I am able to show them in listing. Here below is what I have done to be able to achieve it so far.

plugins\technobrave\properties\models\property\columns.yaml

columns:
    id:
        label: Property Address
        type: property_address
        searchable: true
        sortable: false    

plugins\technobrave\properties\Plugin.php

public function registerListColumnTypes()
    {

        return [
            // A local method, i.e $this->evalUppercaseListColumn()
            'property_address' => [$this, 'evalPropertydDetailsListColumn'],        
        ];
    }

public function evalPropertydDetailsListColumn($value, $column, $record)
{


    $property_array_data = array();
    $current_property = Property::where('id', $record->id)->first();   

    if($current_property)
    {
        if( ($current_property->lot != NULL) || ($current_property->lot != '') )
        {
            $property_array_data[] = $current_property->lot;
        }

        if( ($current_property->unit != NULL) || ($current_property->unit != '') )
        {
            $property_array_data[] = $current_property->unit;
        }

        if( ($current_property->street_number != NULL) || ($current_property->street_number != '') )
        {
            $property_array_data[] = $current_property->street_number;
        }


        if( ($current_property->po_box != NULL) || ($current_property->po_box != '') )
        {
            $property_array_data[] = $current_property->po_box;
        }


        if( ($current_property->street != NULL) || ($current_property->street != '') )
        {
            $property_array_data[] = $current_property->street;
        }


        if( ($current_property->street_type_id != NULL) || ($current_property->street_type_id != '') )
        {
            $get_street_type_data = StreetType::where('id', $current_property->street_type_id)->first();
            $property_array_data[] = $get_street_type_data->street_name;
        }


        if( ($current_property->state_id != NULL) || ($current_property->state_id != '') )
        {
            $get_state_data = State::where('id', $current_property->state_id)->first();
            $property_array_data[] = $get_state_data->state_name;
        }

        if( ($current_property->suburb_id != NULL) || ($current_property->suburb_id != '') )
        {
            $get_suburb_data = Suburb::where('id', $current_property->suburb_id)->first();
            $property_array_data[] = $get_suburb_data->suburb;
        }


        if( ($current_property->post_code != NULL) || ($current_property->post_code != '') )
        {
            $property_array_data[] = $current_property->post_code;
        }



        $imp_property_data = implode(' ', $property_array_data);


        return $imp_property_data;
    }









}

I just need a help to be able to search the above address field when i search the records in my search-box ..

Any idea ?

Thanks


Solution

  • OK guys,

    Eventually I have come up with something to achieve this.

    From plugins\technobrave\properties\models\property\columns.yaml and plugins\technobrave\properties\Plugin.php

    I simply put below code in my columns.yaml file.

    plugins\technobrave\properties\models\property\columns.yaml

    columns:    
        unit:
            label: Property Address
            type: text
            searchable: true
            sortable: false
            invisible: true
            select: CONCAT(COALESCE(`lot`,''),' ',COALESCE(`unit`,''),' ',COALESCE(`street_number`,''),' ',COALESCE(`po_box`,''),' ',COALESCE(`street`,''), ' ', COALESCE( (SELECT technobrave_streets_.street_name FROM technobrave_streets_ WHERE technobrave_streets_.id=street_type_id), ''), ' ', COALESCE( (SELECT technobrave_states_.state_name FROM technobrave_states_ WHERE technobrave_states_.id=state_id), ''), ' ', COALESCE( (SELECT technobrave_suburbs_.suburb FROM technobrave_suburbs_ WHERE technobrave_suburbs_.id=suburb_id), ''), ' ', COALESCE(`post_code`,'') )
    

    As you see in above code, now I am getting property address through SELECT query itself by using CONCAT and also put searchable as true to make it searchable.

    Hope this helps.