Home > Software engineering >  Pivot tables with laravel
Pivot tables with laravel

Time:02-15

im new in laravel and php and im working on a project that has a specific requirements. I would appreciate if you would help me get into right direction. Use case is:

I need a project for my client that's gonna track balances between warehouses, generating documents and updating balances and transfers between ones. Every item in warehouse should have its own LOT code (ex 2 items that received in warehouse can have different codes and i need to update balance for that particular LOT code but still be able to show balance sum for that item with all LOT codes). Users with right permission should have access to only 1 warehouse (and admin should have access for all), and be able to transfer items to another by which LOT code would be generated. So lets give an example:

User 1 has access to warehouse Storage and Finished products; User 2 has access to warehouse Preparation;

Balance of Storage for item sugar is 1000kg with LOT code "28012022"; User 1 receives 500kg of ex sugar with LOT code "02022022" (or autogenerated if no input), he enters all the data about that reception in fields. He then sends 200kg with LOT code 28012022 and 100kg with "02022022" to Preparation warehouse. User 2 confirms transaction and Storage balance is updated with new values subtracted with that amounts, and Preparation warehouse is updated with new item values and new LOT code is generated (28012022/02022022)

User 2 then enters spent amounts and how much of Finished products is made from it, his balance is updated with balance left and LOT code (in this example 28012022/02022022). Then user 2 commits transaction and user 1 confirms it and Finished Products balance is updated with new generated LOT code (28012022/02022022/currentDate);

My question is, should i use pivot tables in laravel, and as im using spatie laravel permission, how should i implement that so that i can give access to some users only for certain warehouses? Any pointers in right direction will be appreciated. Here is git of a project: https://github.com/cile1993/cmss

CodePudding user response:

This is simple project. All you have to do is to make User->warehouse relation. Since user can have many warehouses and warehouse can have many users you need many to many.

users
    id - integer
    name - string
 
warehouses
    id - integer
    name - string
 
warehouse_user
    user_id - integer
    warehouse_id - integer

User Class:

class User extends Model
{
    /**
     * The warehouses that belong to the user.
     */
    public function warehouses()
    {
        return $this->belongsToMany(Warehouse::class);
    }
}

Warehouse class:

class Warehouse extends Model
{
    /**
     * The users that belong to the warehouse.
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

Then if you want to give user access to warehouse just do :

$warehouse = App\Warehouse::find(1);
$warehouse->users()->attach($user_id);

or other way around

$user = App\User::find(1);
$user->warehouses()->attach($warehouse_id);
  • Related