0

I am trying to this data through this code Auth::user()->personnel->currentAssignment->role->is_allowed but I am getther null value because the suppose role value return assignment, instead of role data.

This is working fine Auth::user()->personnel->currentAssignment so far, but once I join the role, Im getting PersonnelAssignment data instead of RoleCatelog which causes to null value

please Im new to Laravel. I need your help.

As you can see it return this data:

"current_assignment": {
            "id": "778",
            "personnel_id": "100751",
            "area_id": 154,
            "role_id": 17,
            "dept_id": 154,
            "start_date": "2017-11-08",
            "end_date": null,
            "is_temporary": null,
            "is_deleted": null,
            "modify_id": "Sample Name",
            "modify_dt": null,
            "create_id": "Administrator",
            "create_dt": null,
            "transfer_id": null,
            "transfer_dt": null,
            "role": {
                "id": "101",
                "personnel_id": null,
                "area_id": 154,
                "role_id": 17,
                "dept_id": 154,
                "start_date": "2007-05-04",
                "end_date": null,
                "is_temporary": null,
                "is_deleted": null,
                "modify_id": "Sample Name II",
                "modify_dt": null,
                "create_id": "Sample Name II",
                "create_dt": null,
                "transfer_id": null,
                "transfer_dt": null
            }
        }

This for assignment model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;

class PersonnelAssignment extends Model
{
    protected $table = 'smed_personnel_assignment';

    protected $primaryKey = 'id';
    protected $keyType = 'string';
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = [
        'id',
        'personnel_id',
        'area_id',
        'role_id',
        'dept_id',
        'start_date',
        'end_date',
        'modify_id',
        'modify_dt',
        'create_id',
        'create_dt',
    ];


    public function role(){
        return $this->belongsTo(RoleCatalog::class, 'role_id');
    }

}

And this is for role model...

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RoleCatalog extends Model
{
    protected $table = 'smed_personnel_assignment';

    protected $primaryKey = 'role_id';
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = [
        'role_id',
        'role_name',
        'role_desc',
        'role_area',
        'is_allowed',
        'is_deleted',
        'modify_id',
        'modify_dt',
        'create_id',
        'create_dt',
    ];


}

..

  • 1
    You should try to post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), if you have problem with the last two models: `currentAssignment->role->is_allowed`, it's better to post only that two models and the minimal code to reproduce the problem. We agree that `Auth::user()->personnel->currentAssignment` is working right? Not everyone here has the spare time to read all that code and you risk of not getting a good answer. – dparoli Aug 24 '19 at 20:16
  • Is there maybe a method `is_allowed` in `RoleCatalog` class? Could you also show the migration file for `RoleCatalog`? – Adam Aug 24 '19 at 23:16
  • @Adam Im not using a migration for now, since Im not yet familiar on how to do it. Yes, there is ```is_allowed```, i have posted also the RoleCatalog model. But i am getting the data from the assignment instead. Am I wrong using belongsTo for this case? – BONIX SULOD Aug 25 '19 at 00:40
  • @dparoli I have updated my post, thanks for the tip. – BONIX SULOD Aug 25 '19 at 01:10

1 Answers1

0

Seen that the primary key on the RoleCatalog model is role_id I would try to change the relationships as follow:

class PersonnelAssignment extends Model
{    
    public function role(){
        return $this->belongsTo(RoleCatalog::class, 'role_id', 'role_id');
    }
}

class RoleCatalog extends Model
{
    public function personnelAssignment(){
        return $this->hasMany(PersonnelAssignment::class, 'role_id', 'role_id');
    }
}

And BTW I would change the table name of the RoleCatalog model, now it is set to the same table of PersonnelAssignment; both models has protected $table = 'smed_personnel_assignment';

.

dparoli
  • 8,891
  • 1
  • 30
  • 38