php - Laravel Eloquent bad building query
Get the solution ↓↓↓I built an Eloquent query like the following.
DB::connection()->enableQueryLog();
$reportSelect = Payment::join('users', 'users.id', '=', 'payment.user_id')
->join('user_information', 'user_information.user_id', '=', 'users.id')
->join('register_type', 'register_type.id', '=', 'payment.register_type_id')
->join('payment_type', 'payment_type.id', '=', 'payment.payment_type_id')
->select(
'payment.id',
'user_information.full_name as user_full_name',
'register_type.register_type',
'payment_type.payment_type',
'payment.created_at',
'payment.updated_at'
);
$date_start = '2021-7-6';
$date_end = '2021-7-10';
$reportSelect->whereBetween('payment.created_at', [$date_start, $date_end])->get();
$queries = DB::getQueryLog();
return $queries;
The result of this SQL query log is the following.
SELECT
'payment'.'id',
'user_information'.'full_name' as 'user_full_name',
'register_type'.'register_type',
'payment_type'.'payment_type',
'payment'.'created_at',
'payment'.'updated_at'
FROM 'payment'
inner join 'users' on 'users'.'id' = 'payment'.'user_id'
inner join 'user_information' on 'user_information'.'user_id' = 'users'.'id'
inner join 'register_type' on 'register_type'.'id' = 'payment'.'register_type_id'
inner join 'payment_type' on 'payment_type'.'id' = 'payment'.'payment_type_id'
WHERE 'payment'.'created_at' between ? and ?
Why not set in the where clause mydate_start
param anddate_end
param, instead it puts the character?
. Thanks in advance; sorry for my English.
Answer
Solution:
//You need to add date method with $date_start and $date_end
$date_start = date('2021-7-6'); $date_end = date('2021-7-10');
$reportSelect->whereBetween('payment.created_at', [$date_start, $date_end])->get();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.