Ad
Laravel Joining Tables In A Database Query
I have this code:
$classic_games_money = DB::table('bets')
->where('user_id', $this->user->id)
->sum('price');
It displays the amount of income, but i need to display this information only if the user id indicated in the winner_id
column in games
table. That is, how can I connect another table in this query?
Ad
Answer
You don't need join
here, exists
will be enough. I guess you have game_id
column in bets
table.
$classic_games_money = DB::table('bets')
->where('user_id', $this->user->id)
->whereExists(function ($query) {
$query
->selectRaw(1)
->from('games')
->whereRaw('games.id = bets.game_id')
->whereRaw('games.winner_id = bets.user_id');
})
->sum('price');
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad