I want to complete one big query from different methods (private method one + private method two .. = public method which merges all of the other) in Laravel. Problem is I do not know how to handle Laravel bindings in query builder because there is no documentation for these functions.
I have tried using Laravel DB::query()
methods addBinding
, setBindings
. I have found out that my code works fine when I'm using function setBindings
all at one time but that is not what I want.
namespace Repository;
use Illuminate\Support\Facades\DB;
use \DateTime;
class QueryBuilder {
/**
* @param DateTime $date
*
* @return \Illuminate\Database\Query\Builder
*/
public function ExtendedBasicQueryBuilder(DateTime $date)
{
$parameters = [
'minDate:' => $date->format('Y-m-d H:m:s'),
];
$query =
$this->basicQueryBuilder()
->where('date', '<', ':minDate')
->addBinding($parameters)
;
return $query;
}
/**
* @return \Illuminate\Database\Query\Builder
*/
private function basicQueryBuilder()
{
$parameters = [
':stateActive' => 1,
];
$query =
DB::query()
->select('id')
->from('items')
->where('state', '=', ':stateActive')
->addBinding($parameters)
;
return $query;
}
}
And the result query will be
string(70) "select `id` from `items` where `state` = ':stateActive' and `date` < 1"
How to bind all of them properly?