2

Good day, I have this SQL statement and I'm having a difficulty on how to convert this in Laravel Query.

I read the docs in http://laravel.com/docs/4.2/queries and I'm confused. Any help would do.

SELECT count(*) FROM `transactions` WHERE `borrower_id` = 2 
            AND `book_id` = 2 AND (
                (
                    `reservedDate` IS NOT NULL 
                    and `borrowedDate` IS NULL
                ) 
                OR (
                    `borrowedDate` IS NOT NULL 
                    AND `returnedDate` IS NULL
                )
            )

4 Answers 4

1

So I try to build this query by reading the documentations from Laravel. Still haven't tested it yet but hope it could give you the right direction.

Maybe next time, you should show your own query (it's ok if it's not correct). It's easier to get start from there.

$super_query = DB::table('transactions')->where('borrower_id' , '=' , 2)->where('book_id', '=' , 2)->where( function ( $query ) {
$query->where(function ($query1) {
    $query1->whereNotNull('reservedDate')->whereNull('borrowedDate')
)->orWhere(function ($query2) {
    $query2->whereNotNUll('borrowedDate')->whereNull('returnedDate')
})  
})->count();
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I'll show next my query, next time...I freaked out.
Why downvote? Any idea to improve this answer? Thanks
@yowza if you ok with the anser just mark it as correct answer. Thanks
@yowza, my answer is the earliest one, and the one that you marked as correct looks exactly the same :)
oh, I'm sorry about that mate...I change the mark now (:, thank you!
1
    DB::table('transactions')
  ->Where('borrower_id', '=', 2)
  ->Where('book_id', '=', 2)
  ->where(function($query)
  {
    $query->where(function($query1) {
      $query1->whereNotNull('reservedDate')
             ->whereNull('borrowedDate');

    })
    ->orWhere(function($query2) {
      $query2->whereNotNull('borrowedDate')
             ->whereNull('returnedDate');
  });
})->count();

try this.. this will result into

select count(*) from `transactions` where `borrower_id` = 2 and `book_id` = 2 and ((`reservedDate` is not null and `borrowedDate` is null) or (`borrowedDate` is not null and `returnedDate` is null))

4 Comments

Man, you beat me by 30 seconds!
@JaredEitnier I'm still new here... just signing up to help... i don't know this is a contest
Oh no, it's not a contest per-se, just a friendly game of who can get the checkmark first :) Welcome.
@JaredEitnier Good game, well played... hahah
1

Try this:

Eloquent

Transactions::where('borrower_id', '=', 2)
    ->where('book_id', '=', 2)
    ->where(function($query)
    {
        $query->whereNotNull('reservedDate')
                ->whereNull('borrowedDate');
    })
    ->orWhere(function($query)
    {
        $query->whereNotNull('borrowedDate')
                ->whereNull('returnedDate');
    })
    ->count();

Comments

0
book::count()->where('borrower_id','=','2','book_id','=',2)

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.