如何从父记录中查找子表的对应记录

时间:2019-09-21 18:03:03

标签: laravel laravel-5 eloquent backpack-for-laravel

我正在laravel做一个项目,并使用背包制作管理面板。我有一个名为用户的表和另一个表user_details,其中用户表的主键用作外键。 我的问题是我如何从父表ID访问特定用户的详细信息。 在laravel auth和背包中,我可以做类似的事情

auth()->user()->id;

或者对于我可以使用的背包

backpack()->user()->id;

类似地,我认为我可以通过执行以下操作来获取user_details页面的所有列:

backpack()->user()->user_details;

但是这种方式行不通吗?

这是我的app \ User.php模型

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Http\Controllers\Role;
use Illuminate\Notifications\Notifiable;
use App\Models\Student;

class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;
protected $guard_name = 'web';

public function Student(){
    return $this->hasMany('App\Models\Student');
}
public function Exam(){
    return $this->hasMany('App\Models\Exam');
}
public function Result(){
    return $this->hasMany('App\Models\Result');
}
public function Fee(){
    return $this->hasMany('App\Models\Fee');
}

/**
 * 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',
];
}

和app \ Models \ Student.php模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Student extends Model
{
    use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $table = 'student_details';
// protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $guarded = [];
// protected $hidden = [];
// protected $dates = [];

/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function User()
{
    return $this->belongsTo('App\Models\User');
}
public function ClassRoom()
{
    return $this->belongsTo('App\Models\ClassRoom');
}
public function Exam(){
    return $this->hasMany('App\Models\Exam');
}
public function Result(){
    return $this->hasMany('App\Models\Result');
}
public function Fee(){
    return $this->hasMany('App\Models\Result');
}
/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| ACCESORS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我已经回答了我的问题,所以我将其发布在这里,以便其他人可以从中获得帮助:实际上问题出在我的模型中。父子模型之间的关系应如下:

父模型:

class User extends Model
{
    public function Student()
    {
        return $this->hasOne(Student::class);
    }
}

子模型:

class Student extends Model
{
    public function User()
    {
        return $this->belongsTo(User::class);
    }
}