-1

I am doing a project in laravel and using backpack for making admin panel. I have a table named users and another table user_details in which primary key of users table is used as a foreign key. My question is that how can i access details of a particular user from parent table id. In laravel auth and backpack, I can do stuff like

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

or for backpack I can use

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

Similarly I thought i would be able to get all columns of user_details page by doing this:

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

But it's not working this way ??

Here is my app\User.php Model

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

and app\Models\Student.php model :

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

Any help will be greatly appreciated!

1 Answers1

1

I have got the answer to my question so I am posting it here so that other may get help from it: Actually problem was in my models. The relationship between parent and child model should be as following:

Parent Model:

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

Child Model:

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