-1

Raw query to Laravel Query

SELECT * FROM plans_subscriptions WHERE starts_on BETWEEN "2019-07-30" AND "2019-07-26" or expires_on BETWEEN "2019-07-20" AND "2019-07-22"
4
  • laravel.com/docs/5.8/queries Commented Jul 2, 2019 at 7:37
  • this link might help Commented Jul 2, 2019 at 7:41
  • What exactly is your question? Commented Jul 2, 2019 at 9:15
  • You are expected to put some effort in solving your problem yourself. If you have tried something but are stuck then share what you have tried and we can work with you from there. Dumping a query and asking "raw query to laravel query" is not even a real question. It's more like a demand. Commented Jul 2, 2019 at 9:53

3 Answers 3

0

If you want object response then use this :

PlansSubscriptions::whereBetween('starts_on', ["2019-07-30", "2019-07-26"]);
                 ->orWhereBetween('expires_on', ["2019-07-20", "2019-07-22"]);
                 ->get();

If you want array response then use this :

PlansSubscriptions::whereBetween('starts_on', ["2019-07-30", "2019-07-26"]);
                 ->orWhereBetween('expires_on', ["2019-07-20", "2019-07-22"]);
                 ->get()->toArray();

We can format the date like this :

$start = date("Y-m-d",strtotime($request->input('from_date')));
$end = date("Y-m-d",strtotime($request->input('to_date')));

and use that variable in query like this:

PlansSubscriptions::whereBetween('starts_on', [$start, $end]);
                 ->orWhereBetween('expires_on', ["2019-07-20", "2019-07-22"]);
                 ->get()->toArray();
Sign up to request clarification or add additional context in comments.

4 Comments

can we use above query with and rather than or
here we are using or with second whereBetween condition can we use and with this
Like this SELECT * FROM plans_subscriptions WHERE starts_on BETWEEN "2019-06-02" AND "2019-07-26" and expires_on BETWEEN "2019-07-20" AND "2019-07-22"
Yes, just remove the or from orWhereBetween and small w Like this: "PlansSubscriptions::whereBetween('starts_on', ["2019-07-30", "2019-07-26"])->whereBetween('expires_on', ["2019-07-20", "2019-07-22"]); ->get()->toArray();"
0
 PlansSubscriptions::whereBetween('starts_on', ["2019-07-30", "2019-07-26"]);
                 ->orWhereBetween('expires_on', ["2019-07-20", "2019-07-22"]);
                 ->get();

Comments

0

Check this,

$users = DB::table('plans_subscriptions')
->where(function ($query){
  $query->whereBetween('starts_on', ['2019-07-30', '2019-07-26'])
        ->orWhereBetween('starts_on', ['2019-07-20', '2019-07-22']);
})
->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.