2
public function create(Request $request){
    $comment = new Comment;
    $comment->user_id = Auth::user()->id;
    $comment->post_id = $request->id; //here is where the response is string
    $comment->comment = $request->comment;
    $comment->save();
    $comment->user;

    return response()->json(
        $comment,
    );
}

I dont know why the reponse is string id and in the database migration the post_id table is $table->unsignedBigInteger('post_id'); it suppose to be integer..

{
"user_id": 4,
"post_id": "2", //response is string 
"comment": "test string id finale",
"updated_at": "2020-10-31T19:55:49.000000Z",
"created_at": "2020-10-31T19:55:49.000000Z",
"id": 11,
}
3
  • 2
    Cast value to int with (int). All values in request are strings. Commented Oct 31, 2020 at 20:26
  • ok this works...but how is it coming to string? Commented Oct 31, 2020 at 20:29
  • 1
    Because client sends to server strings. Commented Oct 31, 2020 at 20:33

1 Answer 1

5

Go to your Comment model and add:

    protected $casts = [
    'post_id' => 'integer',
    ];

And the string (that always gets sent over in a request) should be converted to int on the model (and then end up in JSON as a number).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.