Php 使用“;仅限();功能在拉威尔雄辩
我有两个表Php 使用“;仅限();功能在拉威尔雄辩,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有两个表user和post 在my posts方法中,我希望返回带有自定义文件的用户帖子。 以下解决方案都不起作用 class UserController { public function posts(User $user) { return $user->only(['username', 'name', 'posts.body' // solution one return $user->only(['username', 'name', 'pos
user
和post
在my posts方法中,我希望返回带有自定义文件的用户帖子。
以下解决方案都不起作用
class UserController
{
public function posts(User $user)
{
return $user->only(['username', 'name', 'posts.body' // solution one
return $user->only(['username', 'name', 'posts'=>function($q){
$q->select(['body']
}])// solution two
有人有解决办法吗?在post中,一行只获取一个列值,然后使用
value
方法
Post::where('user_id')->value('body')
Post::where('user_id')->pluck('body') //this will get on the array
如果要从单个列中获取多个值(行),请使用pull
方法
Post::where('user_id')->value('body')
Post::where('user_id')->pluck('body') //this will get on the array
否则使用选择方法
Post::where('user_id')->value('body')
Post::where('user_id')->pluck('body') //this will get on the array
您希望返回哪些自定义字段?@JigneshJoisar body field from posts table您期望的输出是什么?返回类型应该是User
还是数组?@apokryfos返回类型应该是具有自定义Posts
字段关系的User