Getting error as "array_merge(): Argument #2 is not an array"

Published 3 months ago by inzmam

When i am going to delete the comment from post, i am getting error as "array_merge(): Argument #2 is not an array". I am unable to guess the source of this error. Can someone help me this case.

lufemo

Verify if all arguments passed in array_merge are arrays, because all arguments needs to be arrays.

inzmam

Thanks for your reply, i don't have any array in fact.

Probabbly i'm getting error here.

public function destroy($id) {

    $comment=Comment::find($id);
    $post_id=$comment->post->id;
    $comment->delete();
    Session::flash('success','your comment has been deleted');
    return view('posts.show',$post_id);
}           
lufemo

I didn't understand the relation between array_merge and this code block. The $post_id aren't returned to view?

lostdreamer_nl

the second argument in the view() function should be an array, but you're putting an INT in there.

Change your code to this:

public function destroy($id) {

    $comment=Comment::find($id);
    $post_id=$comment->post->id;
    $comment->delete();
    Session::flash('success','your comment has been deleted');
    return view('posts.show', compact('post_id'));
}           

And in your posts.show view you can now use the $post_id variable.

inzmam

$post_id is grabbing the id of the post where it will be returned once the comment is deleted from the post. Actually when i'm deleting the comment, it shows me this error and when i'm going back to the page where i had clicked to delete the comment, it shows me a message as your comment has been deleted. I will share few more code that might help you to figure out the issue.

delete.blade.php

Do you really want to delete this comment?

{{Form::model($comment,['route'=>['comments.destroy',$comment->id],'method'=>'DELETE'])}} {{Form::submit('Delete',['class'=>'btn btn-danger btn-block'])}} {{Form::close()}} CommentsController.php

public function delete($id) { $comment=Comment::find($id); return view('comments.delete')->withComment($comment);

}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    
    $comment=Comment::find($id);
    $post_id=$comment->post->id;
    $comment->delete();
    Session::flash('success','your comment has been deleted');
    return view('posts.show',$post_id);
}

route: Route::delete('comments/{id}',['uses'=>'[email protected]','as'=>'comments.destroy']); Route::get('comments/{id}/delete',['uses'=>'[email protected]','as'=>'comments.delete']);

below given code is where i start the process of deleting the comment.

inzmam

Thanks for your reply, i have used what you told but now i have got a new error as undefined variable 'post'

lostdreamer_nl

Yes, not so strange, I just saw that you are trying to load the posts.show view (probably the view that actually shows the $post data) But you dont have the post there.

So 2 ways of changing:

  1. After the destruction of the comment, you redirect the user back to where they came from.
  2. You show the posts.show view, but also load in the post again.

I would go for the first.

public function destroy($id)
{
    
    $comment=Comment::find($id);
    $post_id=$comment->post->id;
    $comment->delete();
    Session::flash('success','your comment has been deleted');
    return redirect()->back();
}

The second would look like this: (but watch out, users can now press F5 and get a new error because you try to delete a comment that doesnt exist anymore)

public function destroy($id)
{
    
    $comment=Comment::find($id);
    $post=$comment->post;
    $comment->delete();
    Session::flash('success','your comment has been deleted');
    return view('posts.show',compact('post'));
}
hardeep_singh4u

For Testing Purpose............

inzmam

i have tried the code shared by, but same has not helped me.

inzmam

Actually i totally don't understand why array_merge()???

lostdreamer_nl

when you call the function view() with a second argument, the view function will array_merge that second argument into the array with variables that are already available to the view.

"i have tried the code shared by, but same has not helped me." This does not help us either.... we need to know what went wrong when you changed something.

Try either of my above suggestions, I would suggest the redirect one.

inzmam

I have tried the redirect one also that also does not help me it shows as trying to get property of non-object which means item gets deleted but same is not redirected to the page

Please sign in or create an account to participate in this conversation.