gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:55:51 25 4
gpt4 key购买 nike

我在登录的用户和他想要加入工作的事件之间建立关系时遇到一些问题。我确实采取了许多步骤来完成与我对角色用户所做的类似的事情。但在这里我遇到了一些问题,因为我的 user 使用模型 User,events 使用模型 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();
}
}

模型保存事件

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

最佳答案

您不需要模型来保存事件。这是多对多关系,因此请使用数据透视表。

更改 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);

关于php - Laravel 5.4 关系不起作用无法附加第二个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43044482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com