如何解决如何从两个以上的表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');
}
}
Test1模型:
class Test1 extends Model
{
protected $table = 'tests_1';
protected $fillable = [
'user_id','test_a','test_b',];
public function user()
{
return $this->belongsTo('App\User');
}
}
Test2模型:
class Test2 extends Model
{
protected $table = 'tests_2';
protected $fillable = [
'user_id',];
public function user()
{
return $this->belongsTo('App\User');
}
}
Test3模型:
class Test3 extends Model
{
protected $table = 'tests_3';
protected $fillable = [
'user_id',];
public function user()
{
return $this->belongsTo('App\User');
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。