1

How can I query all users whose last donation is more than 90 days. This is my query but when it is executed I get an error unknown column.

      $value = Session::get('key');
      $dateToday = new DateTime();
      $data = Users::where('userType','=', 'user' )
                ->where($dateToday->format('m/d/Y') - 'lastDonation', '=', 1)
                ->whereNotIn('username', [$value ])
                ->get();
1
  • To save your time, you could check your code by using dd($data->toSql()) and remove ->get() to re-check your query builder Commented Jan 29, 2016 at 3:48

2 Answers 2

1
$data = Users::where('userType','=', 'user' )
->where('lastDonation', '<', Carbon::now()->subDays(90))
->whereNotIn('username', [$value ])
->get();
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the TIMESTAMPDIFF mysql function to compute the difference between two datetimes.

To do that you need to add a clause using whereRaw

$value = Session::get('key');
$dateToday = new DateTime();
$data = Users::where('userType', '=', 'user')
          ->whereRaw('TIMESTAMPDIFF(DAY, lastDonation, NOW()) > 90')
          ->whereNotIn('username', [$value])
          ->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.