laravel - 如何从两个以上的表 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');
}
}
解决方案
推荐阅读
- sql - 从 Oracle 中每个给定的当前日期开始滚动 7 个日历日期
- javascript - can someone explain me this logic in redux-form validation?
- reporting-services - 使用表达式在报表服务器中的 SSRS 中钻取
- spring-boot - 通过工厂方法进行 Bean 实例化失败
- angularjs - AngularJS:在指令后插入模板中的字符串
- javascript - 将数据发送到该 servlet 后,如何使用 ajax 从 servlet 获得响应?
- java - 数组列定位和不停的do...while循环
- angular - Promise returned from onSearch(),onSearchButtonClick() is ignored
- python - Using Python Click within a class
- android - 如何使用 MVVM 在两个活动之间发送意图数据