簡體   English   中英

Laravel 5.4關系不起作用無法附加第二個參數

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

在我登錄的用戶與他想加入的活動之間建立聯系時遇到了一些問題。 我實際上正在采取許多步驟來執行與角色用戶相似的操作。 但是這里有一些問題,因為我的用戶使用模型User, 事件使用模型HomeModel和其他3個模型建立關系並連接到表SaveEvent。 有我的代碼。 我希望有人能告訴我該如何解決的,因為我浪費了2天來解決自己的問題:

型號首頁

 <?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; } } 

模型使用者

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

模型SaveEvent

 <?php namespace App; use Illuminate\\Database\\Eloquent\\Model; class SaveEvent extends Model { public function users() { return $this->belongsToMany('App\\User'); } } 

表對關系

 +------------+------------------+------+-----+---------+----------------+ | 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 | | +------------+------------------+------+-----+---------+----------------+ 
控制器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(); } 

您不需要模型來保存events 因此,使用數據透視表是多對多關系。

User模型中更改event方法,如下所示:

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

並在HomeModel

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

現在您可以使用$user->events->attach($events_id);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM