我在 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
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,...
uj5u.com熱心網友回復:
仔細看看你的模型:
return $this->belongsToMany(Event::class, 'event_user', 'event_id', 'user_id');
和
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id');
雖然表是正確的,但您需要在其中之一上交換 event_id 和 user_id 的順序...
uj5u.com熱心網友回復:
看起來您交換了(外)鍵:
在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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330774.html
