programming:phplaravel

php Artisan tinker example user:

# $laravel artisan tinker 
# list all method of reflection 
$methods = (new ReflectionClass('\App\Models\User'))->getMethods();
 
$u=User::find(5);
 
show $u # show source
dump $u # show data
show App\Models\User # show source code

 
$u=User::with(['digitalCertificates','roles'])->find(5);
 
# find user with releations and add role 
$u=User::with(['digitalCertificates','roles'])->find(8);
$u->roles()->save(App\Models\Role::find(2));
$u->refresh();
 
 
App\Models\Role::create(['id' => 2,   'code' => "archivist",'name' => "Архивист"]);
 
 
Storage::disk('tinker')->put('users_roles.json', json_encode($data_u, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE));
 
# execute code in tinker from file
include('/var/www/laravel/storage/app/tinker/update_users.php')
 
####### - обновление роли 
#найти пользователя 
User::with(['roles:id,name'])->where('id','305')->get(['id','name']);
User::with('digitalCertificates','roles')->where('name','LIKE', '%Ворончихин%')->first()
User::with('Roles')->where('name','LIKE', '%Шестакова%')->get(['id','name'])
User::with('Roles:id,name')->where('name','LIKE', '%Долматова%')->first(['id','name'])
 
 
#Обновление
User::where('name','LIKE', '%тампель%')->first()->roles()->detach(Role::where(['code' => 'user'])->first());
User::where('name','LIKE', '%Ворончихин%')->first()->roles()->syncWithoutDetaching(Role::where(['code' => 'user'])->first());
User::find(450)->roles()->syncWithoutDetaching(Role::where(['code' => 'user'])->first());
 
 
function addUser($uid) {
  return User::find($uid)->roles()->syncWithoutDetaching(Role::where(['code' => 'user'])->first());	
}

Get All Models

SQL create php tinker

  • programming/phplaravel.txt
  • Last modified: 2022/09/19 16:57
  • by admin