Laravel JsonResource:array_merge_recursive():参数 #2 不是数组
Laravel JsonResource: array_merge_recursive(): Argument #2 is not an array
我有一个 JsonResource
的 Post
,应该 return 一个 post。但是在加入其他一些数据后,我得到了这个错误:array_merge_recursive(): Argument #2 is not an array
.
这个不起作用:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($slug)
{
// $post = Post::findOrFail($id);
$post = Post::where('slug', $slug)->first();
// return single post as resource
return new PostResource($post);
}
当我直接 return $posts
时,我得到一个 json 回来,几乎没问题。但它不包含连接数据 comment
.
这里是 class Post extends JsonResource
.
public function toArray($request)
{
// return parent::toArray($request);
$img = '.'.pathinfo('storage/'.$this->image, PATHINFO_EXTENSION);
$imgName = str_replace($img,'', $this->image);
$img = $imgName.'-cropped'.$img;
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'excerpt' => $this->excerpt,
'image' => asset('/storage/' . $this->image),
'image_small' => asset('storage/' . $img),
'author_id' => $this->author_id,
'category_id' => $this->category_id,
'seo_title' => $this->seo_title,
'slug' => $this->slug,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'status' => $this->status,
'featured' => $this->featured,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user' => User::find($this->author_id),
'commentCount' => $this->comment->where(['status' => 1, 'id_post' => $this->id])->count(),
];
}
// **Big mistake below here**:
public function with($request)
{
// return [
// 'version' => '1.0.0',
// ];
}
型号:
class Post extends Model
{
public $primary_key = 'id';
public $foreign_key = 'id_post';
public function user()
{
return $this->belongsTo('App\User', 'id_author', 'id');
}
public function comment()
{
return $this->belongsTo('App\Comment', 'id', 'id_post');
}
}
为什么我会收到有关 array_merge_recursive() 的警告?
我不想重现你的代码的问题,但是 - 你确定你包含了所有内容吗?查看 https://laravel.com/docs/5.6/eloquent-resources#writing-resources 可以定义额外的数据数据也将被 returned 像这样:
/**
* Get additional data that should be returned with the resource array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function with($request)
{
return [
'meta' => [
'key' => 'value',
],
];
}
因此,当我向此 Post
资源 class 添加以下方法时,我能够重现该问题:
public function with($request)
{
return 'test';
}
如您所见,它 return 只是字符串而不是数组,然后我遇到了与您相同的错误。
但是当我根本没有实现这个方法或者当我 return 只是一个数组时,一切都很好。
所以总而言之 - 确保您没有 with
方法定义 return 除了数组之外的其他东西。
我有一个 JsonResource
的 Post
,应该 return 一个 post。但是在加入其他一些数据后,我得到了这个错误:array_merge_recursive(): Argument #2 is not an array
.
这个不起作用:
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($slug)
{
// $post = Post::findOrFail($id);
$post = Post::where('slug', $slug)->first();
// return single post as resource
return new PostResource($post);
}
当我直接 return $posts
时,我得到一个 json 回来,几乎没问题。但它不包含连接数据 comment
.
这里是 class Post extends JsonResource
.
public function toArray($request)
{
// return parent::toArray($request);
$img = '.'.pathinfo('storage/'.$this->image, PATHINFO_EXTENSION);
$imgName = str_replace($img,'', $this->image);
$img = $imgName.'-cropped'.$img;
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'excerpt' => $this->excerpt,
'image' => asset('/storage/' . $this->image),
'image_small' => asset('storage/' . $img),
'author_id' => $this->author_id,
'category_id' => $this->category_id,
'seo_title' => $this->seo_title,
'slug' => $this->slug,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'status' => $this->status,
'featured' => $this->featured,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user' => User::find($this->author_id),
'commentCount' => $this->comment->where(['status' => 1, 'id_post' => $this->id])->count(),
];
}
// **Big mistake below here**:
public function with($request)
{
// return [
// 'version' => '1.0.0',
// ];
}
型号:
class Post extends Model
{
public $primary_key = 'id';
public $foreign_key = 'id_post';
public function user()
{
return $this->belongsTo('App\User', 'id_author', 'id');
}
public function comment()
{
return $this->belongsTo('App\Comment', 'id', 'id_post');
}
}
为什么我会收到有关 array_merge_recursive() 的警告?
我不想重现你的代码的问题,但是 - 你确定你包含了所有内容吗?查看 https://laravel.com/docs/5.6/eloquent-resources#writing-resources 可以定义额外的数据数据也将被 returned 像这样:
/**
* Get additional data that should be returned with the resource array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function with($request)
{
return [
'meta' => [
'key' => 'value',
],
];
}
因此,当我向此 Post
资源 class 添加以下方法时,我能够重现该问题:
public function with($request)
{
return 'test';
}
如您所见,它 return 只是字符串而不是数组,然后我遇到了与您相同的错误。
但是当我根本没有实现这个方法或者当我 return 只是一个数组时,一切都很好。
所以总而言之 - 确保您没有 with
方法定义 return 除了数组之外的其他东西。