Agregado role_id a User
This commit is contained in:
parent
bed975e944
commit
fc367c05a3
4 changed files with 66 additions and 27 deletions
|
@ -16,7 +16,7 @@ class User extends Authenticatable
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name', 'email', 'password',
|
'name', 'email', 'password', 'role_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
43
database/migrations/2025_05_15_023829_agregar_rol_a_user.php
Normal file
43
database/migrations/2025_05_15_023829_agregar_rol_a_user.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use App\UserRole;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AgregarRolAUser extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->foreignId('role_id');
|
||||||
|
});
|
||||||
|
|
||||||
|
$barrio = UserRole::where('nombre', 'barrio')->first();
|
||||||
|
$admin_barrio = UserRole::where('nombre', 'admin_barrio')->first();
|
||||||
|
$comision = UserRole::where('nombre', 'comision')->first();
|
||||||
|
User::all()->each(function($user) use ($barrio, $comision, $admin_barrio) {
|
||||||
|
$user->role_id = $user->is_admin ? $admin_barrio->id :
|
||||||
|
($user->is_compras ? $comision->id : $barrio->id);
|
||||||
|
$user->save();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('user', function (Blueprint $table) {
|
||||||
|
$table->dropForeign('role_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Helpers\CsvHelper as CsvHelperAlias;
|
use App\Helpers\CsvHelper;
|
||||||
|
use App\GrupoDeCompra;
|
||||||
|
use App\User;
|
||||||
|
use App\UserRole;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
class GrupoDeCompraSeeder extends Seeder
|
class GrupoDeCompraSeeder extends Seeder
|
||||||
|
@ -14,9 +16,10 @@ class GrupoDeCompraSeeder extends Seeder
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$registros = CsvHelperAlias::getRecords('csv/barrios.csv');
|
$registros = CsvHelper::getRecords('csv/barrios.csv');
|
||||||
$gdcToInsert = [];
|
$gdcToInsert = [];
|
||||||
$usersToInsert = [];
|
$usersToInsert = [];
|
||||||
|
$roles = UserRole::where('nombre', 'barrio')->orWhere('nombre', 'barrio_admin')->get();
|
||||||
|
|
||||||
foreach($registros as $key => $registro){
|
foreach($registros as $key => $registro){
|
||||||
$gdcToInsert[] = [
|
$gdcToInsert[] = [
|
||||||
|
@ -27,29 +30,21 @@ class GrupoDeCompraSeeder extends Seeder
|
||||||
'referente_finanzas' => $registro['referente']
|
'referente_finanzas' => $registro['referente']
|
||||||
];
|
];
|
||||||
|
|
||||||
$usersToInsert[] = [
|
foreach($roles as $role) {
|
||||||
'name' => $registro['barrio'],
|
$usersToInsert[] = [
|
||||||
'password' => Hash::make("123"),
|
'name' => $registro['barrio'],
|
||||||
"is_admin" => 0,
|
'password' => Hash::make("123"),
|
||||||
'grupo_de_compra_id' => $key
|
'role_id' => $role->id,
|
||||||
];
|
'grupo_de_compra_id' => $key,
|
||||||
|
'is_admin' => $role->nombre != 'barrio' ? 0 : 1,
|
||||||
$usersToInsert[] = [
|
];
|
||||||
'name' => $registro['barrio'] . "_admin",
|
}
|
||||||
'password' => Hash::make("123"),
|
|
||||||
"is_admin" => 1,
|
|
||||||
'grupo_de_compra_id' => $key
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (array_chunk($gdcToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
foreach (array_chunk($gdcToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
||||||
{
|
GrupoDeCompra::insert($chunk);
|
||||||
DB::table('grupos_de_compra')->insert($chunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (array_chunk($usersToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
foreach (array_chunk($usersToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
||||||
{
|
User::insert($chunk);
|
||||||
DB::table('users')->insert($chunk);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use App\UserRole;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
@ -16,16 +18,15 @@ class UserSeeder extends Seeder
|
||||||
$usersToInsert = [];
|
$usersToInsert = [];
|
||||||
|
|
||||||
$usersToInsert[] = [
|
$usersToInsert[] = [
|
||||||
'name' => 'compras',
|
'name' => 'comi',
|
||||||
'password' => Hash::make("123"),
|
'password' => Hash::make("123"),
|
||||||
|
'role_id' => UserRole::where('nombre', 'comision')->first()->id,
|
||||||
'is_admin' => 0,
|
'is_admin' => 0,
|
||||||
'is_compras' => 1
|
'is_compras' => 1
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
foreach (array_chunk($usersToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
foreach (array_chunk($usersToInsert,DatabaseSeeder::CHUNK_SIZE) as $chunk)
|
||||||
{
|
User::insert($chunk);
|
||||||
DB::table('users')->insert($chunk);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue