Support the ongoing development of Laravel.io →
Database Eloquent

I'm currently working on a browser game built with Laravel to get into laravel more.

I'm currently running into a problem now I want to get a highscore listing. The list should contain:

Character level (Saved in table: character) Username (Saved in user table) And clan name if the user has one. (Saved in clan table)

To link a clan to a user I have a table user_has_clan which contains the user_id and clan_id.

I am currently stuck with the query to get the clan name. My entire function looks like:

public function level()

{
	$characters = DB::table('user')

	->where('user.id', '>', 0)

	->join('character', 'user_id', '=', 'character.user_id')

        ->whereExists(function($query)
        {

            $query->select(DB::raw(1))

            ->join('clan', 'clan.id', '=', 'user_has_clan.clan_id')

                  ->from('user_has_clan')

                  ->whereExists('clan.id = user_has_clan.clan_id');
        })

        ->get();

	foreach ($characters as $character) {

		$currentxp = $character->experience;
		$xp = 100;
		$xp2 = 100;
		$levels = 200;
		if($currentxp < 100)
		{
			$character->level = 1;
		}
		elseif($currentxp < 208)
		{
			$character->level = 2;
		}
		else
		{
			for ($i = 2; $i <= $levels; $i++)
			{
				$xp2 = (($xp2 * 104) / 100);
				$xp = (($xp * 104) / 100) + $xp2;
				
				if(ceil($xp) > $currentxp)
				{
					$character->level = $i;
					break;
				}
			}
		}
	}
	return View::make('highscores.level')
	->with('characters', $characters);
        }

I receive the following error when I visit the page: Argument 1 passed to Illuminate\Database\Query\Builder::whereExists() must be an instance of Closure, string given, called in G:\xampp\htdocs\app\controllers\HighscoresController.php on line 15 and defined

Last updated 3 years ago.
0

you should look into Relationships if you want to learn Laravel http://laravel.com/docs/eloquent#relationships

if you set it up correctly you can use stuff like

$user = User::with('characters', 'clan')->where('username', '=', 'Zenry')->first();

$user->characters; // all characters belonging to the user
$user->clan; // dito
Last updated 3 years ago.
0

zenry said:

you should look into Relationships if you want to learn Laravel http://laravel.com/docs/eloquent#relationships

if you set it up correctly you can use stuff like

$user = User::with('characters', 'clan')->where('username', '=', 'Zenry')->first();

$user->characters; // all characters belonging to the user $user->clan; // dito

The problem is, the user doesnt always have a clan, and if none found laravel would throw an error. A character is created upon registration of the account. But a clan is not. The code above will search for a clan that is connected to the user. Therefor I will need a query with a WhereExists function in it if I am correct? Or will the code above NOT show an error once there is no clan connected to the user?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.