简体   繁体   English

Laravel 5.4关系不起作用无法附加第二个参数

[英]Laravel 5.4 relationship doesnt work cant attach second parameter

I've got some problems making relationship between my user who is logged in and events that he wants to join work. 在我登录的用户与他想加入的活动之间建立联系时遇到了一些问题。 I am really taking many steps to do something similar to what I do with role users. 我实际上正在采取许多步骤来执行与角色用户相似的操作。 But here I have some problems, because my user uses model User, events uses model HomeModel and 3 other models to make a relation and connecte to table SaveEvent. 但是这里有一些问题,因为我的用户使用模型User, 事件使用模型HomeModel和其他3个模型建立关系并连接到表SaveEvent。 There is my code. 有我的代码。 I hope someone can tell me how to figure it out because I wasted 2 days to resolve my problem: 我希望有人能告诉我该如何解决的,因为我浪费了2天来解决自己的问题:

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 模型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 | | +------------+------------------+------+-----+---------+----------------+ 
Controller ZapisController 控制器ZapisController

  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(); } 

You don't need a model to save events . 您不需要模型来保存events This is a many to many relationship with so use a pivot table. 因此,使用数据透视表是多对多关系。

Change event method in User model like this : User模型中更改event方法,如下所示:

 public function events()
    {
        return $this->belongsToMany(HomeModel::class,'PIVOT_TABLE_NAME','user_id','events_id');->withTimestamps();
    }

and in HomeModel : 并在HomeModel

 public function users()
    {
        return $this->belongsToMany(User::class,'PIVOT_TABLE_NAME','events_id','user_id');
    }

now you can use $user->events->attach($events_id); 现在您可以使用$user->events->attach($events_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM