mercredi 9 novembre 2016

Laravel 5.3 Polymorphic Relations using Role Type with different fields for user

this is my first time asking a question on stackoverflow

So I want to implement a role based user based on my Multiple Store POS project

For Example:

User -> has a role of -> Manager

So every manager has assets and assets -> for example is a store

This Snippet of Code is inserted on my User.php file in laravel as an Eloquent Model

public function roles()
{
    return $this->belongsToMany(Role::class);
}

public function assets() {
    return $this->hasMany(Asset::class);
}

This snippet of code is based on the Asset.php file

protected $fillable = ['name','description','image_url'];

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

public function assetable()
{
    return $this->morphTo();
}

snippet for the Role.php

protected $fillable = ['name'];

public function users()
{
    return $this->belongsToMany(User::class);
}

im using this trait for polymorph purposes

trait Assetable
{
public function assets()
{
    return $this->morphMany(Asset::class, 'assetable');
}
}

and this is a sample of an asset as i said before snippet for Store.php

class Store extends Model
{
use Assetable;

protected $timestamps = false;

public function stocks() {
    return $this->hasMany(Stock::class);
}

}

So my problem is i want to separate the assets based on the role or this design for database is a bad IDEA?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire