首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel雄辩/许多关系给出错误(数组到字符串转换)

问Laravel雄辩/许多关系给出错误(数组到字符串转换)
EN

Stack Overflow用户
提问于 2021-11-28 14:33:47
回答 1查看 163关注 0票数 0

我想显示用户申请的所有职务帖子。

这是我的桌子..。

代码语言:javascript
复制
applies   ->  | users_id | posts_id |
posts     ->  | id       | (other posts cols ... )
user_info ->  | id       | (name col etc...)

我尝试过belongsToMany(),但给出了错误

mb_strpos():参数#1 ($haystack)必须是字符串类型,数组给定

Post模型关系

代码语言:javascript
复制
public function applies()
{
   return $this->belongsToMany(Applies::class ,'applies', 'users_id' , 'posts_id');
}

应用模型

代码语言:javascript
复制
   protected $table = 'applies';
    protected $primaryKey = ['user_id', 'id'];
    public $incrementing = false;
    protected $fillable = [
        'user_id',
        'posts_id'
    ];

最后控制器

代码语言:javascript
复制
public function index()
{
    $infos = Info::where('user_id', Auth::id())->first();
    $apply = Post::find(2)->applies ;
    var_dump($apply);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-28 14:49:45

必须将枢轴表名作为第二个参数传递给belongsToMany。

代码语言:javascript
复制
 return $this->belongsToMany(User::class ,'applies', 'users_id' , 'posts_id');

如果将实现方法.second参数视为表名

代码语言:javascript
复制
  public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
                                  $parentKey = null, $relatedKey = null, $relation = null)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70144385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档