-
Notifications
You must be signed in to change notification settings - Fork 4
Examples
Benyamin Khalife edited this page Nov 18, 2023
·
5 revisions
Example: Get the number of rows along with the total value of the percent field
$result = UserActivity::select(function($s){
$s->sum('percent')->as('sum');
$s->count()->as('count');
})
->where('client_id', $client->id)
->first();
SELECT SUM(`percent` as 'sum'), count(*) as 'count' WHERE `client_id` = :client_id
Output:
{"sum": 160, "count": 3}
Examples for creating more complex and nested conditions
DB::table('messages')->where('credit', '<', 0)
->where(function($w){
$w->where(function($s){
$s->where('from_user_id',$user_a_id)->or('to_user_id', $user_b_id);
});
$w->or(function($s){
$s->where('from_user_id',$user_a_id)->or('to_user_id', $user_b_id);
});
})
SELECT FROM `messages` where `credit` < 0 AND ((`from_user_id` = :user_a_id OR `to_user_id` = :user_b_id) or (`from_user_id` = :user_b_id OR `to_user_id` = :user_a_id))