I have made a JSON search engine for our APIs.
The logic for assembling queries is as follows;
Laravel macro is made to enable search method on Eloquent models which creates an instance of the class and runs the search method:
/**
* @var $this Builder
*/
$searcher = new Searcher($this, $request);
$searcher->search();
To keep things separated, request parameter classes which have URL query logic are instantiated dynamically and have the same query builder as a dependency. Inside foreach loop the following is executed:
new $parameter($this->request, $this->builder, $this->modelConfig);
In order to see what was actually searched for, I have set the log to write the actual query and bindings
Log::info('[Search] SQL: ' . $this->builder->toSql() . " Bindings: " . implode(', ', $this->builder->getBindings()));
Now the issue I'm having is that when loading related models (using ->with()
in one of the request parameters (RelationsParameter
) that I do get what I asked for, so model is loaded together with relations passed to it, however that is never reflected in the query.
I will always end up with SELECT * FROM table WHERE something
, without the mention of the related table. Is there a way to get the underlying query as well?