Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Laravel 5.4 Nadawanie roli dla użytkowników
Forum PHP.pl > Forum > PHP
K3n0
Witam,
Próbuje zrobić nadawanie roli dla użytkowników, postępowałem jak w tutorialach pokazane było ale niestety natrafilem na taki błąd:

Call to undefined method Illuminate\Database\Schema\Blueprint::bool()
Komunikat ten pokazuje się, gdy chce zrobic migracje.

Model Role
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4.  
  5. class Role extends Model
  6. {
  7. public function users()
  8. {
  9. return $this->belongsToMany('App\User', 'user_role', 'role_id', 'user_id');
  10. }
  11. }

Model User:
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Notifications\Notifiable;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7.  
  8. class User extends Authenticatable
  9. {
  10. public function roles()
  11. {
  12. return $this->belongsToMany('App\Role', 'user_role', 'user_id', 'role_id');
  13. }
  14. use Notifiable;
  15. /**
  16.   * The attributes that are mass assignable.
  17.   *
  18.   * @var array
  19.   */
  20. protected $fillable = [
  21. 'lastname', 'name', 'phonenumber', 'email', 'password',
  22. ];
  23. /**
  24.   * The attributes that should be hidden for arrays.
  25.   *
  26.   * @var array
  27.   */
  28. protected $hidden = [
  29. 'password', 'remember_token',
  30. ];
  31.  
  32. }

create_role_table
  1. <?php
  2. use Illuminate\Support\Facades\Schema;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Database\Migrations\Migration;
  5.  
  6. class CreateRolesTable extends Migration
  7. {
  8. /**
  9.   * Run the migrations.
  10.   *
  11.   * @return void
  12.   */
  13. public function up()
  14. {
  15. Schema::create('roles', function (Blueprint $table) {
  16. $table->increments('id');
  17. $table->string('name_role');
  18. $table->string('description');
  19. $table->timestamps();
  20. });
  21. }
  22. /**
  23.   * Reverse the migrations.
  24.   *
  25.   * @return void
  26.   */
  27. public function down()
  28. {
  29. Schema::drop('roles');
  30. }
  31. }

create_user_role_table
  1. <?php
  2. use Illuminate\Support\Facades\Schema;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Database\Migrations\Migration;
  5.  
  6. class CreateUserRoleTable extends Migration
  7. {
  8. /**
  9.   * Run the migrations.
  10.   *
  11.   * @return void
  12.   */
  13. public function up()
  14. {
  15. Schema::create('user_role', function (Blueprint $table) {
  16. $table->increments('id');
  17. $table->integer('user_id');
  18. $table->integer('role_id');
  19. $table->timestamps();
  20. });
  21. }
  22. /**
  23.   * Reverse the migrations.
  24.   *
  25.   * @return void
  26.   */
  27. public function down()
  28. {
  29. Schema::drop('user_role');
  30. }
  31. }
IProSoft
To jedyne migracje jakie masz w systemie? Sprawdź dokładnie ścieżkę błędu.
K3n0
Mam jeszcze 2 migracje, a dokładniej to migracje do bazy danych user ale ona jest sprawna oraz migracje do fullcalendara do eventow, która też działa. A jak dokładniej sprawdzić scieżkę błedu bo ten error mi wyskakuje w terminalu i tylko tyle komunikatu.
Pyton_000
W migracji masz

"$table->bool("

Przeszukaj migracje.
K3n0
juz poprawilem to:D
jednak faktycznie w baze danych do kalendarza mialem boola:)
Ale teraz mam następny błąd:
[Illuminate\Database\QueryException] SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cann ot be null (SQL: insert into `user_role` (`role_id`, `user_id`) values (1, ))

Zrobiłem coś takiego: $table->integer('user_id')->unsigned()->nullable();
Poszło, ale user_id wypelnia mi nullem, więc muszę dalej rozkminic o co be
Dzięki za pomoc z tym boolem
Pyton_000
Masz problem przy zapisie. Ten nullable tam wręcz nie może być.

Lepiej pokaż zapisywanie roli
K3n0
Zapisywanie to chyba create_user_role_table a role przez seedy narazie dodaje
RoleSeeder:
  1. <?php
  2.  
  3. use Illuminate\Database\Seeder;
  4.  
  5. use App\Role;
  6.  
  7. class RoleTableSeeder extends Seeder
  8. {
  9. /**
  10.   * Run the database seeds.
  11.   *
  12.   * @return void
  13.   */
  14. public function run()
  15. {
  16. $role_user = new Role();
  17. $role_user->name_role = 'User';
  18. $role_user->description = 'Normal User';
  19. $role_user->save();
  20.  
  21. $role_moderator = new Role();
  22. $role_moderator->name_role = 'Moderator';
  23. $role_moderator->description = 'Moderator';
  24. $role_moderator->save();
  25.  
  26. $role_admin = new Role();
  27. $role_admin->name_role = 'Admin';
  28. $role_admin->description = 'Admin';
  29. $role_admin->save();
  30. }
  31. }

Pyton_000
Nie to.. Tam gdzie masz zapisywanie Roli do użytkownika
K3n0
  1. <?php
  2.  
  3. use Illuminate\Database\Seeder;
  4. use App\Role;
  5. use App\User;
  6.  
  7. class UserTableSeeder extends Seeder
  8. {
  9. /**
  10.   * Run the database seeds.
  11.   *
  12.   * @return void
  13.   */
  14. public function run()
  15. {
  16. $role_user = Role::where('name','User')->first();
  17. $role_moderator = Role::where('name','Moderator')->first();
  18. $role_admin = Role::where('name','Admin')->first();
  19.  
  20. $user = new User();
  21. $user->name = 'Mariusz';
  22. $user->lastname = 'Waniewski';
  23. $user->phonenumber = '664593850';
  24. $user->email = 'mariusz@wp.pl';
  25. $user->password = bcrypt('mariusz');
  26. $user->roles()->attach($role_user);
  27.  
  28. $moderator = new User();
  29. $moderator->name = 'Moderator';
  30. $moderator->lastname = 'Moderator';
  31. $moderator->phonenumber = '555555555';
  32. $moderator->email = 'moderator@wp.pl';
  33. $moderator->password = bcrypt('moderator1990');
  34. $moderator->roles()->attach($role_moderator);
  35.  
  36. $admin = new User();
  37. $admin->name = 'Admin';
  38. $admin->lastname = 'Admin';
  39. $admin->phonenumber = '555555555';
  40. $admin->email = 'mariusz@gmail.com';
  41. $admin->password = bcrypt('mariusz1990');
  42. $admin->roles()->attach($role_admin);
  43. }
  44. }
  45.  


A tutaj link do gita:
https://github.com/K3n0s/szpital/tree/kalendarz

Może tak będzię łatwiej
Pyton_000
Musisz najpierw zapisać usera a potem robić attach.
K3n0
Działa dzięki wielkie.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.