Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Laravel 5.4 zapisywanie do eventu
Forum PHP.pl > Forum > PHP
K3n0
Witam,
Mam do zrobienia zapis do eventu, wcześniej pytałem się od czego zacząć. Teraz wiem, że muszę to zrobić na relacjach. Ale niestety napotykam na błąd:
SQLSTATE[HY000]: General error: 1364 Field 'events_id' doesn't have a default value (SQL: insert into `zapis` (`users_id`, `updated_at`, `created_at`) values (1, 2017-03-25 18:00:59, 2017-03-25 18:00:59))

Z tego co widzę to pierwsza wartość się dodaje, ale niestety druga juz nie chce.
I szczerze to nie wiem jak zrobić ten attach, gdyż 1 model odpowiada za usera, 2 model za tabele odpowiedzialna za relacje, a 3 model za eventy w kalendarzu.
Klikajac na event, gdy chce się zapisać routing dobrze działa pobiera id eventu.

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. use Notifiable;
  11. /**
  12.   * The attributes that are mass assignable.
  13.   *
  14.   * @var array
  15.   */
  16. protected $fillable = [
  17. 'lastname', 'name', 'phonenumber', 'email', 'password', 'user_id' ,
  18. ];
  19. /**
  20.   * The attributes that should be hidden for arrays.
  21.   *
  22.   * @var array
  23.   */
  24. protected $hidden = [
  25. 'password', 'remember_token',
  26. ];
  27. public function roles()
  28. {
  29. return $this->belongsToMany('App\Role', 'user_role', 'user_id', 'role_id');
  30. }
  31. public function hasAnyRole($roles)
  32. {
  33. if (is_array($roles)) {
  34. foreach ($roles as $role) {
  35. if ($this->hasRole($role)) {
  36. return true;
  37. }
  38. }
  39. } else {
  40. if ($this->hasRole($roles)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. public function hasRole($role)
  47. {
  48. if ($this->roles()->where('name', $role)->first()) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. public function events()
  54. {
  55. return $this->belongsToMany('App\HomeModel', 'zapis','users_id','events_id')->withTimestamps();
  56. }
  57. }

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

Model HomeModel-odpowiedzialny za kalendarz
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class HomeModel extends Model
  5. {
  6. protected $table = 'events'; // you may change this to your name table
  7. public $timestamps = true; // set true if you are using created_at and updated_at
  8. protected $primaryKey = 'id'; // the default is id
  9. /**
  10.   * Is it an all day event?
  11.   *
  12.   * @return bool
  13.   */
  14. public function isAllDay()
  15. {
  16. return (bool)$this->day;
  17. }
  18. }

Controller ZapisController
  1. //Zapisywanie sie na event
  2. /**
  3.   * Update the specified resource in storage.
  4.   * @param int $id
  5.   * @return \Illuminate\Http\Response
  6.   *
  7.   */
  8. public function acceptEvent($id)
  9. {
  10. // echo $userId = Auth::id();
  11. // echo '</br>';
  12. // echo HomeModel::find($id);
  13. //$userID = Auth::id();
  14. $events_id = Zapis::find($id);
  15. $users_id = new Zapis();
  16. $users_id->users_id=Auth::id();
  17. $users_id->save();
  18. $users_id->events()->attach($events_id);
  19.  
  20. return redirect()->back();
  21. }

Tablica do relacji:
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| users_id | int(10) unsigned | NO | MUL | NULL | |
| events_id | int(10) unsigned | NO | MUL | NULL | |
+------------+------------------+------+-----+---------+----------------+
k_@_m_i_l
Plik Controller ZapisController - linia 18, przekazujesz nieistniejącą zmienną.
K3n0
to ja wiem ze nie istnieje ale nie wiem jak to naprawic;p
IProSoft
Piszesz na początku, że drugi model odpowiada za event, a próbujesz przypisać go zamiast trzeciego.

Nie mieszaj polski/anigielski i nadawaj normalne nazwy( HomeModel questionmark.gif ) dla klas a sam szybciej połapiesz się w czym problem.
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.