Laravel: 我想显示用户评分数据以及用户和组织的联合表

Laravel: I want to display the User score data together with joint tables of user and organization

我想显示我的用户评分数据以及用户和组织的联合表。我还想使用用户分数 id 显示数据。

userScore 模型:

  <?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
    }
}

用户模型:

  <?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');
    }

}

组织模型:

  <?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'); #if column not found indicate the column name
    }

}

这是 UserScoreController:

      public function userScoreByUser(Request $request, $id){
   
        $userScores = userScore::find($id);
        if(is_null($userScores)){
            return response()->json('User Score not found!', 401);
        }

        $user_scores = DB::table('user_scores')
            ->join('users', 'users.id', '=', 'users.id')
            ->join('organizations', 'organizations.id', '=', 'organizations.id')
            ->select('user_scores.*','users.name','users.email','organizations.org_title')
            ->get()
            ;

            $users = userScore::with('users')->get();
        return response(['message'=>"User Score displayed successfully", 'UserScore'=>$userScores,],200);

    }
}

这是API路线:

Route::resource('userScore','App\Http\Controllers\Api\UserScoreCRUDController');
Route::get('userScoreByOrg/{user_scores_id}','App\Http\Controllers\Api\UserScoreCRUDController@userScoreByUser');

我在显示数据时也遇到了问题,当我获取数据或尝试在邮递员中显示数据时,它 returns 用户评分数据与用户而不是联合表组织名称应该可见的地方。

这是示例响应:

{
    "message": "User Score displayed successfully",
    "UserScore": [
        {
            "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,
            "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
            }
        },
        {
            "id": 3,
            "user_id": 3,
            "quiz_id": 1,
            "module_id": 1,
            "number_of_correct_answers": 12,
            "created_at": "2021-08-12T04:29:46.000000Z",
            "updated_at": "2021-08-12T04:29:46.000000Z",
            "time_started": null,
            "time_finished": null,
            "users": {
                "id": 3,
                "name": "Carl Joshua Lalongbilang",
                "email": "carllalongbilang282@gmail.com",
                "email_verified_at": null,
                "created_at": "2021-08-05T17:16:42.000000Z",
                "updated_at": "2021-08-05T17:16:42.000000Z",
                "provider": null,
                "provider_id": null,
                "avatar": null
            }
        },

它也没有按 userScore ID 显示数据。

我目前是第一次学习 Laravel 所以,任何形式的 help/suggestions 将不胜感激。谢谢!!!

不用join,你试过这样吗?

public function userScoreByUser(Request $request, $id){

    $userScore = userScore::where('id',$id)->with('users')->with('users.organizations')->first();
    if(is_null($userScore)){
        return response()->json('User Score not found!', 401);
    }

    return response(['message'=>"User Score displayed successfully", 'UserScore'=>$userScore,],200);

}