John Davidson

php - Eloquent get user with highest relation table points

0 comments
Message:


I'm trying to get the user with the highest evaluation points based on charisma and persuasion


What i have wrote so far after this i couldn't figure out how to make it work


    $user = User::where('commision_id', $data['commision'])->whereHas('role', function ($q) {
$q->where('level', 2);
})->with(['evaluations' => function ($q) {

}]);

The evaluations relation migration


    Schema::create('evaluations', function (Blueprint $table) {
$table->id();
$table->boolean('charisma')->default(false);
$table->boolean('persuasion')->default(false);
$table->boolean('politics')->default(false);
$table->boolean('heat')->default(false);
$table->string('title');
$table->integer('hl')->nullable();
$table->integer('exp')->nullable();
$table->unsignedBigInteger('user_id');
$table->timestamps();

$table->index('user_id');
});

So basically i have to sum the exp points based on persuasion and the exp points based on charisma then sum both of those sum values to get the highest ranking user.



My Answer:
>withCount(['evaluations as total_points' => function ($query) {
$query->select(DB::raw('SUM(charisma + persuasion) as total_points'));
}])
->orderByDesc('total_points')
->first();

Rate this post

3 of 5 based on 1224 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us