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
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'lastname', 'name', 'phonenumber', 'email', 'password', 'user_id' , ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; public function roles() { return $this->belongsToMany('App\Role', 'user_role', 'user_id', 'role_id'); } public function hasAnyRole($roles) { foreach ($roles as $role) { if ($this->hasRole($role)) { return true; } } } else { if ($this->hasRole($roles)) { return true; } } return false; } public function hasRole($role) { if ($this->roles()->where('name', $role)->first()) { return true; } return false; } public function events() { return $this->belongsToMany('App\HomeModel', 'zapis','users_id','events_id')->withTimestamps(); } }
Model Zapisz
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Zapis extends Model { public function users() { return $this->belongsToMany('App\User', 'zapis','events_id','users_id'); } }
Model HomeModel-odpowiedzialny za kalendarz
<?php namespace App; use Illuminate\Database\Eloquent\Model; class HomeModel extends Model { protected $table = 'events'; // you may change this to your name table public $timestamps = true; // set true if you are using created_at and updated_at protected $primaryKey = 'id'; // the default is id /** * Is it an all day event? * * @return bool */ public function isAllDay() { return (bool)$this->day; } }
Controller ZapisController
//Zapisywanie sie na event /** * Update the specified resource in storage. * @param int $id * @return \Illuminate\Http\Response * */ public function acceptEvent($id) { // echo $userId = Auth::id(); // echo '</br>'; // echo HomeModel::find($id); //$userID = Auth::id(); $events_id = Zapis::find($id); $users_id = new Zapis(); $users_id->users_id=Auth::id(); $users_id->save(); $users_id->events()->attach($events_id); return redirect()->back(); }
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 | |
+------------+------------------+------+-----+---------+----------------+