Hi i got some problems to make relationship between my user who is logged and events what he want to join. I really trying many steps. Do something like i do with role users. But here i got some problems, becouse my user use model User,events use model HomeModel and 3 model is for make relation and connected to table SaveEvent. There is my code i hope someone told me how figure it becouse i wasted 2 days to resolve my problem: Model HomeModel
<?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;
}
}
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)
{
if (is_array($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\SaveEvent')->withTimestamps();
}
}
Model SaveEvent
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SaveEvent extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
Table to relationship
+------------+------------------+------+-----+---------+----------------+
| 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 | |
+------------+------------------+------+-----+---------+----------------+
public function acceptEvent($id)
{
$events_id = HomeModel::find($id);
$users_id = new SaveEvent;
$users_id->users_id=Auth::id();
$users_id->save();
$users_id->events()->attach($events_id);
return redirect()->back();
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire