Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用“;仅限();功能在拉威尔雄辩_Php_Laravel_Laravel 5 - Fatal编程技术网

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