Laravel response is String instead of int

Viewed 1021
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,
}
1 Answers

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).

Related