Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel all return ID become 0

Tags:

php

laravel

I've got a problem with all my queries with a varchar ID returning an integer.

For example,a query which should return C-1451963448-MjL returns instead 0.

My query:

$transaction = Transaction::with('user')->get();
return response()->json($transaction);

which returns:

[
  {
    "id": 0,
    "user_id": 8,
    "amount": "56164.00",
    "created_at": "2016-01-05 10:10:48",
    "user": {
      "id": 8,
      "facebook_id": null,
      "first_name": "Alta",
      "last_name": "Keebler",
    }
  },
]

the transaction id becoming 0. Last time I tried it succeeded, but now every ID with varchar returns 0 instead of the real value.

I've tried to foreach the get query, and yes the id is 0. I also tried to dd($transaction) when i checked it, it return me with correct ID.

What should I do to make it work again?

like image 608
ssuhat Avatar asked Jan 05 '16 03:01

ssuhat


1 Answers

Set $incrementing to false on your model.

public $incrementing = false;

The casts system checks if this is set to true, if it is, it will try to cast the the key field to an int.

like image 131
lagbox Avatar answered Oct 20 '22 04:10

lagbox