我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

Laravel,如何在使用belongsToMany时过滤行?

I'm confused about the Laravel API and its many to many relationship model.

There are the following tables:

posts tags post_tag

And I want to find posts which have tagId = 123

Is there simple source code similar to the following?

$posts= Post::find()->pivot->tag(123);

Thanks.

You should specify the method in Tag model:

public function posts()
{
    return $this->belongsToMany('App\Post');
}

Then you can find posts which have tagId = 123

$posts = Tag::find(123)->posts();

近期文章

  • 保存数据库行的状态
  • mysql join只返回第一行,它应该是多个
  • 将csv文件导入MySQL时,在创建表时更改数据类型 - PHP
  • 如何在codeigniter中加入2表
  • Symfony 3.3模式验证错误
  • 当有人在Mediawiki上编辑一个页面时执行操作
  • 尝试使用数组PHP格式化日期时出错
  • 如何同时进行ajax调用?
  • 从mac中的php运行bash脚本时,$ USER为空
  • 如何在wordpress中自定义自定义发布日期格式?
  • 在PHP中设置ROOT目录
  • $ a =新PharData($ tar_achive); cakephp3
  • 在php中显示mysql的日期
  • 如何将数组对象php显示为ajax js?
  • 无法通过ajax调用显示返回数据
  • 会话变量 - mysqli_query结果
  • 如何获得组织模块信息,vtiger
  • unserialize():13个字节的偏移9处的错误
  • 为什么我的SQL在刷新页面时插入相同的数据?
  • Yii2控制台从外部服务器获取数据

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华