php - 将用户对象绑定到 laravel 中的每篇文章
问题描述
这是在这个例子中我获取所有category
拥有的articles
并将所有文章绑定到每个category
属于这样的文章
$categories = Category::has('articles')->with('articles')->get();
结果
[
{
"id": 1,
"title": "Technology",
"created_at": "2019-09-22 16:58:24",
"updated_at": "2019-09-22 16:58:24",
"articles": []
},
{
"id": 2,
"title": "Programming",
"created_at": "2019-09-22 21:01:39",
"updated_at": "2019-09-22 22:36:17",
"articles": [{
"id": 10,
"title": "Intro To JavaScript",
"content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"image": "articles/OUMvOY17KaqfcfK10c16UAcGegXeESopRw1hlxl9.png",
"user_id": 1,
"category_id": 2,
"created_at": "2019-09-22 22:35:10",
"updated_at": "2019-09-22 22:35:10"
}]
}
]
我需要的是也user
为每个对象绑定article
我该怎么做
解决方案
也许这可以帮助你:
$categories = Category::has('articles')->with('articles' => function($query) {
$query->with('user');
})->get();
希望能帮助到你。
推荐阅读
- flutter - 如何在底部导航中换行
- typescript - 打字稿部分
即使使用 `?` 链接或条件检查也会导致未定义的错误 - ios - iOS:使用领域 SDK 进行交易时崩溃
- typescript - 为什么这个简单的反应原生应用程序的引导屏幕上的图像没有显示?
- android - 制作带有播放控件的蓝牙扬声器连接到手机
- sharepoint-online - Sharepoint Online 列表和 Common Data Service
- vue.js - 当我们使用 nuxt-password-protect 包时,真的需要在我们的 Nuxt 应用程序中使模式通用吗?
- azure-ad-b2c - Azure B2C - 提供的令牌不包含有效的颁发者
- mysql - 第一个表中的所有数据均未显示在 sql 中
- c# - 如果帐户已在 C# 中创建,如何创建创建新用户帐户和登录方法的 Create 方法