首页 > 解决方案 > 如何从两个以上的表 laravel 中获取价值?

问题描述

我对雄辩的 laravel 有问题。我无法从我的表中获取数据。 这是要求:从表 test_1、test_2 和 test_3 中获取列表用户和总和值。 这是我的表:用户表:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('username');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

        });

test_1 表:

Schema::create('tests_1', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedBigInteger('user_id');
            $table->smallInteger('test_a')->nullable();
            $table->smallInteger('test_b')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });

test_2 表:

Schema::create('tests_2', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->smallInteger('test_a')->nullable();
            $table->smallInteger('test_b')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });

test_3 表:

Schema::create('tests_3', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->smallInteger('test_a')->nullable();
            $table->smallInteger('test_b')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });

我的控制器:

$users = User::with(['test1' => function($query){
                        $query->select('*', DB::raw("SUM(test_a + test_b) as score1"));
                    }])
                    ->with(['test2' => function($query){
                        $query->select('*', DB::raw("SUM(test_a + test_b) as score2"));
                    }])
                    ->with(['test3' => function($query){
                        $query->select('*', DB::raw("SUM(test_a + test_b) as score3"));
                    }])
                    ->get();


        return view('home', compact('users'));

我的观点 :

<table>
                        <tr>
                            <th>Name</th>
                            <th>Total Score 1</th>
                            <th>Total Score 2</th>
                            <th>Total Score 3</th>
                        </tr>
                        @foreach($users as $key=> $user)
                        <tr>
                            <td>{{ $user->username }}</td>
                            <td>{{ $user->test->score1 }}</td>
                            <td>{{ $user->test->score2 }}</td>
                            <td>{{ $user->test->score3 }}</td>
                        </tr>
                        @endforeach
                    </table>

我遇到错误并卡在那里。

我的模型: 用户模型:

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'username', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function test1()
    {
        return $this->hasOne('App\Test1');
    }

    public function test2()
    {
        return $this->hasOne('App\Test2');
    }

    public function test3()
    {
        return $this->hasOne('App\Test3');
    }
}

测试1模型:

class Test1 extends Model
{
    protected $table = 'tests_1';
    
    protected $fillable = [
        'user_id', 'test_a', 'test_b',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

测试2模型:

class Test2 extends Model
{
    protected $table = 'tests_2';
    
    protected $fillable = [
        'user_id', 'test_a', 'test_b',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

测试3模型:

class Test3 extends Model
{
    protected $table = 'tests_3';
    
    protected $fillable = [
        'user_id', 'test_a', 'test_b',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

标签: laraveleloquentclosures

解决方案


推荐阅读