源码目录在\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations下

关系:一对多(One To Many)

场景:每篇博客都有若干条评论,每条评论只属于一篇博客

先定义两个Model,这里为了突出主要矛盾,删除了Model中部分无关代码

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $table = 'posts';
public function Comments()
{
return $this->hasMany('App\Comment','PostID','CommentID');
}
}

以上是博客Post的模型,接下来是评论Comment的模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
protected $table = 'comments';
public function Comment()
{
//
} public function Post()
{
return $this->belongsTo('App\Post','CommentID','PostID');
}
}

控制器里:

$articles=Post::where('id','>','50')->where('id','<','52')->get();
foreach($articles as $a){
//echo $a->id;
$comments=$a->Comments;
foreach($comments as $c){
//echo $c->id;
}
}

  

  

  

最新文章

  1. 原生js学习笔记2
  2. Matlab程序 转C++/Opencv基于Mat 不可不知的17个函数
  3. 记录创建企业Wiki的几个开源项目
  4. Summary: How to calculate PI? Based on Monte Carlo method
  5. IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法
  6. sharepoint rest api 创建文档库 文件夹
  7. 支持行单击、双击事件的GridView和DataList控件(译)
  8. hive的map类型处理
  9. tp3.2 上传文件及下载文件
  10. MT【292】任意存在求最值
  11. HDOJ 4267 A Simple Problem with Integers (线段树)
  12. input输入框只能输入数字而且开头不能为零
  13. [LeetCode] 504. Base 7_Easy tag: Math
  14. 学习笔记3—matlab中load特殊用法
  15. iOS.FBTweak
  16. 【转】Mysql学习---SQL的优化
  17. WebAPI 消息处理器
  18. Mac下Homebrew的图形化界面工具Cakebrew
  19. asp.net mvc access数据库操作
  20. linux top命令查看系统资源

热门文章

  1. Selenium关于Page Objects
  2. Java网络编程总结
  3. PHP实现验证码图片
  4. 关于js中for in的缺陷浅析
  5. RAC时间同步的两种方法
  6. 国产数据库-KingbaseES在linux下的安装
  7. Blend4 的安装和配置
  8. jQuery事件绑定、解绑、命名空间
  9. weblogic 集群部署时上传jsp不更新问题
  10. JSP获取绝对物理地址