Rick
Rick

Reputation: 2271

Laravel Model attribute returns 0

I have a Report Model with fillables user_id, complainant_id, title, detail. The table migration also has the same fields, and I have double checked that they are correct.

My problem is when I fetch the reports using Report:all() or any kind of way to fetch the reports. The complainant_id is always 0. But in the database, they actually contain values that are not 0.

enter image description here enter image description here enter image description here

Upvotes: 3

Views: 645

Answers (2)

Manjunath Karamudi
Manjunath Karamudi

Reputation: 100

I was able to see the correct value when using just "model:all()" but when fetching data converting to Array or json(model0->json_decode() or toArray()) for getting single value it returns "0" and then i got to know about model builtin function "getAttributes()" it helped.Below is the Syntax to use. (I hope this will be helpfull for someone...)

 model->getAttributes()['attribute'];

                       

Upvotes: 0

Joey Carlisle
Joey Carlisle

Reputation: 835

I had a similar problem. The issue was I made the string column the primary key.

protected $primaryKey = "column";

When you do this, Laravel casts the column to a int (making everything zero) unless you add this

protected $keyType = "string";
public $incrementing = false;

Documentation

Upvotes: 1

Related Questions