繁体   English   中英

无法从具有不同表名的belongsToMany检索关系

[英]cant retrieve relation from belongsToMany with different tablename

我得到以下表格:

| messages |   | likes      |
| id       |   | id         |
| content  |   | message_id |
               | user_id    |

我想从一条消息中检索所有喜欢的消息。

目前,我在Message模型上具有以下关系:

public function likes()
{
    return $this->belongsToMany('App\Entities\Like', 'likes', 'message_id', 'id');
}

但是当我dd(Message::find($id)->likes()->get()->toArray()); 我有一个例外:

Syntax error or access violation: 1066 Not unique table/alias: 'likes'

您需要定义一个Message模型和Like模型之间有很多关系。

message.php

public function likes()
{
   return this->hasMany('App\Entities\Like');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM