0

Good morning.

I have a question that I can't solve with eloquent, let me put it in context.

I have several farms I need all the subscribers of that farm that are taken out through a pivot table and in turn all the receipts of those subscribers, so far so good. The problem comes in that I put a condition in the dates of the receipts, and I want that it does not extract the subscribers that do not fulfill that condition.

 $estates = Estate::with(['adminEstate.neighbours' => function ($query) use ($past_date_init) {
            $query->with(['receiptHeader' => function ($query2) use ($past_date_init) {
                $query2
                    ->where('receipt_h_billing_init', '<=', $past_date_init)
                    ->where('receipt_h_billing_end', '>=', $past_date_init);
                $query2->with('receiptDetail');
            }
            ]);
        }])->get();

If they do not meet the date condition, I will not be able to get the subscribers.

Thanks for your help

  • Please attach the format of the date in the query condition. also make sure that your `date` fields was set in the models in `$dates` var so laravel will referer your field as a date. – Eliran Givoni Apr 29 '21 at 18:10
  • See here: https://stackoverflow.com/a/49389521/3460080 – Eliran Givoni Apr 29 '21 at 18:11
  • I think you want to filter your base model based on the condition applied to your relationship, means you want to remove `adminEstate.neighbours` which doesn't meet the condition in 'receiptHeader' (in other words doesn't return a dataset after the condition) right ? – Daniyal Nasir Apr 29 '21 at 19:56

1 Answers1

0

Well for now you can use

query2
 ->whereDate('receipt_h_billing_init', '<=', $past_date_init)
 ->whereDate('receipt_h_billing_end', '>=', $past_date_init);

query2
     ->whereBetween(DB::raw('Date(receipt_h_billing_init)'),[$past_date_init,  $past_date_init]);

as you can see here I didn't use whereBetween because it will compare also the time the first one will work fine

PsyLogic
  • 639
  • 4
  • 10