I have a class Roles where I define a static array like this
<?php
namespace App;
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{
public static $roles = [
'admin1aaa' => [
'name' => 'admin2aaa',
'display_name' => 'Usuario admin',
] ,
'secretary' => [
'name' => 'secretary',
'display_name' => 'Usuario de secretaria',
],
'teacher' => [
'name' => 'teacher',
]
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name','display_name','description'
];
/**
* Get all users a role belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany('App\User')->withTimestamps();
}
}
What I'm trying to do is check if the elements of the array
of roles that are in Role::$roles
but the elements in the table that insert them exist, the problem that I have that both Role::$roles
as Role::all()
I have the feeling that will find the array
of the model, and what I need is that in one case it does the array
of the model and in the other case it does so from the table of the data base .. but I do not see how to do it
public function createRoles()
{
$countDone =0;
$countFail =0;
foreach (Role::$roles as $key => $value) {
$role = Role::all()->find('name',$value['name']);
if ($role === null) {
$owner = new Role();
$owner->name = $value['name'];
$owner->display_name = $value['display_name'];
if ($owner->save())
$countDone++;
else
$countFail++;
}
}
return $this->jsonCreatedResponse('Roles created correctly : '.$countDone . ' Failed : '. $countFail);
}