Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

laravel - How to modify a relationship?

My app has 2 main modules which are Foo and Bar. It also has 3 types of role: admin, manager & staff.

Each user is assigned to a supervisor, so that every supervisor will have some subordinates assigned to him/her.

For example, staff1 is supervised by manager1 whom is also supervised by admin1.

The current practice for this relationship is implemented in both modules. Therefore each supervisor is in charge for the subordinates in the matter of their Foo and Bar.

User.php

<?php
namespace App;
use SpatiePermissionTraitsHasRoles;
use IlluminateSupportStr;
class User {
    protected $fillable = ['name','email','password','supervisor_id'];
    protected $appends = ['role'];
    public function getRoleAttribute(){
        return $this->roles[0];
    }
    public function getNameAttribute($value){
        return Str::title($value);
    }
    public function parent(){
        return $this->hasOne('AppUserStructure', 'user_id');
    }
    public function scopeSupervisor($query){
        return $query->where('id', $this->supervisor_id)->first();
    }
    public function foo(){
        return $this->hasMany(Foo::class, 'user_id', 'id');
    }
    public function bar(){
        return $this->hasMany(Bar::class, 'user_id', 'id');
    }
}

UserStructure.php

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class UserStructure extends Model{
    protected $fillable = ['user_id', 'parent_id'];
    public function user(){
        return $this->belongsTo('AppUser', 'user_id');
    }
}

Role.php

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Role extends Model{
    protected $fillable = ['name'];
    public function roles(){
        return $this->belongsTo('AppUser', 'role');
    }
}

RoleAndPermission.php

<?php
use IlluminateDatabaseSeeder;
use SpatiePermissionModelsRole;
use SpatiePermissionModelsPermission;
class RolesAndPermissionsSeeder extends Seeder{
    public function run(){
        $roles = ['admin','manager','staff'];
        app()[SpatiePermissionPermissionRegistrar::class]->forgetCachedPermissions();
        foreach ($roles as $role) {
            Role::updateOrCreate(['name' => $role]);
        }
    }
}

UserSeeder.php

<?php
use IlluminateDatabaseSeeder;
use AppUser;
use AppUserStructure;
class UserSeeder extends Seeder{
    public function run(){
        $items = [
            ['role'=> 'admin',
             'name'=> 'admin',
             'email'=> '[email protected]',
             'password'=> 'password',
             'supervisor_id'=> 1],
            ['role'=> 'manager',
             'name'=> 'manager',
             'email'=> '[email protected]',
             'password'=> 'password',
             'supervisor_id'=> 1],
            ['role'=> 'staff',
             'name'=> 'staff',
             'email'=> '[email protected]',
             'password'=> 'password',
             'supervisor_id'=> 2],
        ];
        foreach($items as $data) {
            $user = User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
                'supervisor_id' => $data['supervisor_id'],
            ]);
            $user->assignRole($data['role']);
        }
        $userStructure = [
            ['parent_id'=> 0, 'user_id'=> 1],
            ['parent_id'=> 1, 'user_id'=> 2],
            ['parent_id'=> 2, 'user_id'=> 3]
        ];
        UserStructure::insert($userStructure);        
    }
}

My question is, how do I modify this relationship accordingly so that any supervisor [admin/ manager] will be assigned to the subordinate [manager/ staff] of one module only?

(E.g: In Foo module, staff1 is supervised by manager1. While in Bar module, he will be supervised by manager2.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...