【问题标题】:Laravel many to many relationship with uuid returns always emptyLaravel 与 uuid 的多对多关系返回总是空的
【发布时间】:2019-10-01 02:24:25
【问题描述】:

我使用 Laravel 5.8 并将模型的自动增量 ID 更改为 uuid。从那时起,我在我的两个模型EventUser(带有数据透视表events_users)之间定义的多对多关系遇到了一些问题。

问题: 现在,当我请求连接两个表的所有元素(我的数据透视表中有 2 条记录)时,我总是得到一个空数组。 调试sql的时候发现where子句参数没有设置:

// Generated sql
select `users`.*, `events_users`.`event_id` as `pivot_event_id`, `events_users`.`user_id` as `pivot_user_id`, `events_users`.`created_at` as `pivot_created_at`, `events_users`.`updated_at` as `pivot_updated_at`
from `users`
inner join `events_users` on `users`.`id` = `events_users`.`user_id`
where `events_users`.`event_id` = ?

// Bindings :
Array
(
    [0] => 
)

有人知道我在这里缺少什么吗?

这是我的模型的定义:

class Event extends Model
{
    protected $primaryKey = 'id';
    protected $keyType = 'string';
    public $incrementing = false;

// here some other model methods, fillable property, etc.


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

}

用户模型的声明相同,但有关系

public function events()
{
    return $this
        ->belongsToMany(Event::class, 'events_users', 'user_id', 'event_id')
        ->withPivot(['created_at', 'updated_at']);
}

然后我从服务中检索关系:

public function getSubscriptions($eventId)
{
    $eventId = 'a1b7c5d6-8f86-44f4-f31a-46e32917d5c0'; // for debug purpose only
    $event = Event::find($eventId);

    foreach ($event->users as $user) {
        print_r($user); die; // It never loops here as its length is 0 but should be 2...
    }

    \DB::listen(function ($query) {
        print_r($query->sql);
        print_r($query->bindings);
        // $query->time
    });

    $subscriptions = $event
        ->users()
        ->get();
    die;

    return $subscriptions;
}

我的数据库包含记录

【问题讨论】:

  • 会不会有其他东西会破坏模型的行为?我基本上已经 1:1 复制了你的代码,而且它在我这边工作得非常好。
  • 有趣,我不知道从哪里开始寻找这个,但我会检查,Tx
  • 可能会覆盖您的 $event->users 属性。我附上了我的数据库结构和测试代码:hastebin.com/ebonawiguj.js & 结果:i.imgur.com/roCGepd.png
  • 谢谢@Dan。它对我找到问题有很大帮助(请参阅此线程中的答案)。我不知道它为什么会出错,但是删除这个 id 初始化就可以了。
  • 您不会绕过臃肿的模型,但您可以使用@method@property 将魔术方法和参数排除到类的DocBlock 中。还有一个由 barryvdh 提供的名为 laravel-ide-helper 的包,它也生成帮助文件和所有必要的 DocBlocks。

标签: laravel laravel-5 eloquent many-to-many relationship


【解决方案1】:

问题在于我的模型中列出了属性的另一个声明。 我在那里初始化了一个id 属性,这可能与uuid 类型冲突,或者我不知道究竟是什么原因导致了这出戏…… 无论如何,删除此行让应用程序正常工作。

/**
 * @var array
 * Rules used for fields validation
 */
public $rules = array(
    'title'       => 'required|string|max:255',
    'start_date'  => 'required|date|date_format:Y-m-d',
    'end_date'    => 'required|date|date_format:Y-m-d|after_or_equal:start_date',
    'location'    => 'string|max:254',
    'latitude'    => 'numeric',
    'longitude'   => 'numeric'
);

public $id          = "";  // This is the line that create the bug... Remove it and it works !
public $title       = "";
public $start_date  = "";
public $end_date    = "";
public $location    = "";
public $latitude    = "";
public $longitude   = "";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多