首页 > 解决方案 > 我想按组织 ID 显示所有具有用户分数的用户

问题描述

我目前正在开发一个测验模块,我想通过使用 org id 来显示特定的数据或测验结果。我已成功检索数据,但每个数组仅显示一个对象。我的目标是显示所有拥有或连接到用户的用户分数。

这是响应的示例:

"message": "User Score displayed successfully",
    "UserScore": [
        {
            "id": 1,
            "org_title": "Sapphire2 Cargo Moving Inc.",
            "org_description": "Lorem Ipsum",
            "org_address": "Metro Manila",
            "users": {
                "id": 1,
                "name": "Juan Dela Cruz",
                "email": "juan@gmail.com",
                "email_verified_at": "2021-07-31T03:31:23.000000Z",
                "created_at": "2021-07-31T03:04:59.000000Z",
                "updated_at": "2021-07-31T03:31:23.000000Z",
                "provider": null,
                "provider_id": null,
                "avatar": null,
                "user_scores": [
                    {
                        "id": 1,
                        "user_id": 1,
                        "quiz_id": 1,
                        "module_id": 1,
                        "number_of_correct_answers": 20,
                        "created_at": "2021-08-12T04:18:18.000000Z",
                        "updated_at": "2021-08-12T04:18:18.000000Z",
                        "time_started": null,
                        "time_finished": null
                    }
                ]
            }
        }
    ]
}

这是它应该是什么的一个例子:

"message": "User Score displayed successfully",
    "UserScore": [
        {
            "id": 1,
            "org_title": "Sapphire2 Cargo Moving Inc.",
            "org_description": "Lorem Ipsum",
            "org_address": "Metro Manila",
            "users": {
                "id": 1,
                "name": "Carl Joshua Lalongbilang",
                "email": "carllalongbilang28@gmail.com",
                "email_verified_at": "2021-07-31T03:31:23.000000Z",
                "created_at": "2021-07-31T03:04:59.000000Z",
                "updated_at": "2021-07-31T03:31:23.000000Z",
                "provider": null,
                "provider_id": null,
                "avatar": null,
                "user_scores": [
                    {
                        "id": 1,
                        "user_id": 1,
                        "quiz_id": 1,
                        "module_id": 1,
                        "number_of_correct_answers": 20,
                        "created_at": "2021-08-12T04:18:18.000000Z",
                        "updated_at": "2021-08-12T04:18:18.000000Z",
                        "time_started": null,
                        "time_finished": null
                    }
                    {
                        "id": 1,
                        "user_id": 1,
                        "quiz_id": 2,
                        "module_id": 2,
                        "number_of_correct_answers": 30,
                        "created_at": "2021-08-12T04:18:18.000000Z",
                        "updated_at": "2021-08-12T04:18:18.000000Z",
                        "time_started": null,
                        "time_finished": null
                    }
                ]
            }
        }
    ]
}

这是组织模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class organizations extends Model
{
    public $table = "organizations";
    use HasFactory;

    protected $guarded = [];

    public function users(){
        // return $this->belongsToMany('App\Models\User','user_id'); #if column not found indicate the column name
          return $this->belongsTo('App\Models\User',"id"); #if column not found indicate the column name
    }

    public function userScores(){
        // return $this->belongsToMany('App\Models\User','user_id'); #if column not found indicate the column name
          return $this->belongsTo('App\Models\userScore'); #if column not found indicate the column name
    }
}

这是 user_score 模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class userScore extends Model
{
    public $table = "user_scores";
    use HasFactory;

    protected $guarded = [];

    public function users(){
        return $this->belongsTo('App\Models\User','user_id'); #if column not found indicate the column name
    }

    public function organizations(){
        return $this->belongsTo('App\Models\organizations','id'); #if column not found indicate the column name
    }

    public function userScores(){
        return $this->belongsTo('App\Models\userScore','id'); #if column not found indicate the column name
    }
}

这是用户模型:

<?php

namespace App\Models;
use Spatie\Permission\Models\Role;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable, HasApiTokens, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function socialAccount(){
        return $this->hasMany(SocialAccount::class);
    }

    
    public function organizations(){
        return $this->hasMany('App\Models\organizations','id'); #if column not found indicate the column name
    }

    public function userScores(){
        return $this->hasMany('App\Models\userScore','id');
    }

}

这是 UserScore 控制器:

    public function userScoreByOrgID(Request $request, $org_id){

            $OrgScore = organizations::where('id',$org_id)
            ->with('users.userScores')
            ->select('organizations.id','organizations.org_title','organizations.org_description',
            'organizations.org_address')
            ->get();
            if(is_null($OrgScore)){
                return response()->json('User Score not found!', 401);
            }
        
            return response(['message'=>"User Score displayed successfully", 'UserScore'=>$OrgScore,],200);
    //     return response(['message'=>"User Score displayed successfully", 'User Score'=>$members],200);
    // }
}

我目前正在第一次学习 Laravel 所以,任何形式的帮助/建议都将不胜感激。谢谢!!!

标签: laraveleloquentpostmanbackend

解决方案


推荐阅读