Laravel JsonResource:array_merge_recursive():参数#2不是数组
我有一个Laravel JsonResource:array_merge_recursive():参数#2不是数组,laravel,laravel-5,laravel-5.6,laravel-response,laravel-resource,Laravel,Laravel 5,Laravel 5.6,Laravel Response,Laravel Resource,我有一个JsonResource的Post应该返回一个Post。但是在加入一些其他数据之后,我得到了这个错误:array\u merge\u recursive():参数2不是数组 此不起作用: /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($slug) { // $post = Post
JsonResource
的Post
应该返回一个Post。但是在加入一些其他数据之后,我得到了这个错误:array\u merge\u recursive():参数2不是数组
此不起作用:
/**
* 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);
}
当我直接返回$posts
时,我得到了一个json,几乎没有问题。但是它不包含连接的数据注释
下面是类文章扩展了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\u merge\u recursive()的警告?我不想在您的代码中重现这个问题,但是-您确定包含了所有内容吗?查看是否有可能定义其他数据,数据也将返回如下所示:
/**
* 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
资源类添加以下方法时,我能够重现这个问题:
public function with($request)
{
return 'test';
}
正如你们看到的,它只返回字符串而不是数组,然后我得到了和你们一样的错误
但是当我根本没有实现这个方法,或者当我只返回一个数组时,一切都很好
总而言之,请确保您没有使用定义的
方法返回数组以外的内容。我不想用您的代码重现这个问题,但是-您确定您包含了所有内容吗?查看是否有可能定义其他数据,数据也将返回如下所示:
/**
* 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
资源类添加以下方法时,我能够重现这个问题:
public function with($request)
{
return 'test';
}
正如你们看到的,它只返回字符串而不是数组,然后我得到了和你们一样的错误
但是当我根本没有实现这个方法,或者当我只返回一个数组时,一切都很好
总之,请确保您没有定义带有
方法的返回数组以外的内容。就是这样。我用()注释了中的代码,认为它没有用,但我应该注释整个函数。干得好,非常感谢!就是这样。我用()注释了中的代码,认为它没有用,但我应该注释整个函数。干得好,非常感谢!