Reputation: 4045
I was wondering if someone could tell me how to write the following query using L4's query builder.
SELECT u.user_id,c.c_id,u.username,u.email
FROM conversation c, users u
WHERE CASE
WHEN c.user_one = '$user_one'
THEN c.user_two = u.user_id
WHEN u.user_two = '$user_one'
THEN c.user_one= u.user_id
END
AND (
c.user_one ='$user_one'
OR c.user_two ='$user_one'
)
Order by c.c_id DESC
I cant seem to find anything in the L4 doc's about CASE
Cheers,
Upvotes: 0
Views: 1357
Reputation: 2539
Laravel 4 does not have support for this using the Query Builder
. However you can use the general select
function as such
$params = array($user_one, $user_one, $user_one, $user_one);
$results = DB::select(
"SELECT u.user_id, c.c_id, u.username, u.email
FROM conversation c, users u
WHERE
CASE
WHEN c.user_one = ?
THEN c.user_two = u.user_id
WHEN u.user_two = ?
THEN c.user_one = u.user_id
END
AND (
c.user_one = ?
OR c.user_two = ?
)
ORDER BY c.c_id DESC",
$params
);
You can read more about this on Laravels database documentation.
Upvotes: 1