lundi 15 juillet 2019

Laravel - Eloquent gets more resultas than same SQL code in DB

I have a query made with Laravel Query Builder. I did notice that reulsts was including some unexpected records (not included in the query). WHen i call to ->toSql() and try it in database directly, the record number is different.

Query Builder Code:

public function getItems() : Collection
    {
        return Item::whereBetween(
            'created_at', 
            [
                $this->attributes['start_date'],
                $this->attributes['end_date']
            ]
        )->whereIn('customer_code', $this->customers()->pluck('code')->toArray())
        ->whereIn('code', explode(",", $this->attributes['products']))
        ->get();
    }

Mysql Query:

select * 
from `items` 
where (`created_at` between "2019-06-03 02:00:00" and "2019-07-04 01:59:00") 
and `customer_code` in ("1234567890","0987654321","123098465","159357826","4862183579","123654823") 
and `product_code` in ("8470001838919","8470001838902","8470001674241","8470001593160","8470001748577","8470001839695","8429420128248","8470001902870")

This query gets 156 records, and mysql db query get 151. I did realize when I got some code in empty string (not included in whereIn clause). What am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire