laravel - Laravel 多对多关系不起作用
问题描述
我在 Laravel 关系中遇到了一个奇怪的问题。
我想访问一个有很多用户的活动(完美)。
<?php
$event_with_user = Event::with('registered_users')->where('id', 253)->get();
dd($event_with_user);
我想访问所有连接事件的用户:
<?php
$user_with_event = User::with('registered_events')->where('id', 1)->get();
dd($user_with_event);
我总是收到此错误:
Illuminate\Database\Eloquent\RelationNotFoundException “调用模型 [App\User] 上的未定义关系 [registered_events]。”
我多次检查关系,但找不到错误。还有其他人有这个问题吗?
我的模特
用户.php
<?php
namespace App;
use App\Event;
use App\UserType;
use App\EventUser;
use App\Department;
use App\Evaluation;
use App\UserLocation;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'fname', 'lname', 'email', 'password', 'phone', 'email_verified_at', 'admin', 'user_type_id', 'last_login_at', 'last_login_ip', 'user_location_id', 'user_notification_type_id', 'user_notification_setting_id', 'slack_user_id', 'joinpoints_user_id'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function registered_events()
{
return $this->belongsToMany(Event::class, 'event_user', 'event_id', 'user_id');
}
}
事件.php
<?php
namespace App;
use App\User;
use App\EventRoom;
use App\EventType;
use App\EventStatus;
use App\EventLocation;
use App\EventParticipant;
use App\EventParticipantItems;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class Event extends Model
{
use Sluggable;
protected $fillable = [
'event_type_id', 'user_id', 'status', 'customer', 'slug', 'datetime', 'duration','embed_chat','number_of_participants','heading', 'description_1', 'description_2', 'livestream_link', 'logo_link', 'event_password', 'participants_per_room', 'youtube_id', 'embed_code', 'session_link', 'hide_breakout', 'help_link', 'lang', 'layout', 'btn2_text', 'btn2_link', 'help_text', 'background_url', 'black_font',
];
protected $dates = ['datetime'];
public function getRouteKeyName()
{
return 'slug';
}
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable()
{
return [
'slug' => [
'source' => 'customer'
]
];
}
public function registered_users()
{
return $this->belongsToMany(User::class, 'event_user', 'user_id', 'event_id')->withPivot('id', 'user_status', 'eventmanager', 'created_at', 'updated_at');
}
}
我的表:
- 用户身份,....
- 事件:ID,...
- event_user: id, user_id, event_id,...
解决方案
看起来你交换了(外)键:
在User.php中:
public function registered_events()
{
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id');
}
在Event.php 中:
public function registered_users()
{
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id')->withPivot('id', 'user_status', 'eventmanager', 'created_at', 'updated_at');
}
当您遵循正确的 Laravel 数据库结构时,您可以简单
地return $this->belongsToMany(Event::class);
放入User.php和
return $this->belongsToMany(User::class)->withPivot(...);
Event.php
推荐阅读
- dart - 如何在飞镖 FFI 中使用布尔类型?
- python - 如何在数据中多次枚举函数循环
- python - Django:成功登录()后,下次刷新request.user页面时,它再次变为“匿名”
- php - 文件名无效 Filezilla localhost
- laravel - 带下拉菜单的自动字段数据
- google-cloud-platform - 如何使用 oauth/jwt 保护我的 http 触发的 GCP 功能?
- c# - 为什么 List 条目执行的函数的结果不是持久的?
- html - CSS 绝对位置和 webKitMask 剪切内容
- python - 具有客户用户模型的 DRF 多用户
- python - Flask 未显示字段错误