Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cemcminn's avatar

Advanced Database Searching (Ad hoc querying)

I am developing my first Laravel Project.

The project that I will be building is a web version of a FileMaker legacy system.

In FileMaker, the user is able to search all fields included on the layout in order to display the accounts he/she wants to work with. The database is quite complex with at least 7 to 8 data tables with many fields to maintain for each account (I would say there is 200+ in all). I would prefer to build a search panel -- I do not relish the idea of having search and edit in one set of fields -- unless that is part of Laravel that I need to embrace :-)

I am looking for a design pattern to follow that might be most effective in a case like this in Laravel.

I found this github project: https://github.com/amochohan/advanced-eloquent-search-filters

Is this probably my best route? Anyone else have another suggestion for a design pattern/search setup I should pursue?

Thank you!

0 likes
3 replies
jlrdw's avatar

I usually have a search form at top of table, something like: https://drive.google.com/file/d/0B1_PFw--3o74YjVreHNBOWU2aEE/view

And something like:

$query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);

        $params = array('psch' => $dogsearch, 'aval' => $aval);

Notice:

      $params = array('psch' => $dogsearch, 'aval' => $aval);

You have to pass your parameter array to your paginator appends method.

Also have you looked at https://laravel.com/docs/5.7/scout

cemcminn's avatar

@JLRDW - Thank you! Scout looks like something I should perhaps look into. My goal is to allow a flexible query building process on as many of the fields as possible. Translation: Not so much coding and more dynamic -- especially if if I add new fields down the road.

Before I decided to use Laravel, I had actually almost built something using MySQL schema info tables and the comment field. Here is a snippet or two:

Array to hold comparison types:

private $operators_arr = ['='=>'equals', '!='=>'not equal to', '>'=>'greater than', '>='=>'greater than or equal to', '<'=>'less than', '<='=>'less than or equal to', 'like'=>'like', 'like'=>'begins with',
                              'contains'=>'contains'];

Array to hold form field types (html):

private $answer_type_arr = ['textbox'=>'1', 'datepicker'=>'3', 'yn_bit'=>'4', 'dd_specific'=>'5'];

Then in the comments of the field, I planned to add 'Field Label|1' -type conventions for those fields which should be searchable.

I built stored procedures to access the table/column information that started something like this:

select concat(table_name, '.', column_name) as column_name, column_comment from 
information_schema.columns where (table_name like ...) and COLUMN_NAME not in (place to leave out columns that are not to be searched) or COLUMN_NAME not !=;

Then I had built a form that could be hidden and displayed on the management page and additional rows added as needed (with plus sign/minus sign buttons) to build the search parameters for the search.

cemcminn's avatar

I looked at Scout. I don't see that does anything other than full-text searching. Am I missing something?

Please or to participate in this conversation.