10 months ago

string field for Primary Key goes data error

Posted 10 months ago by binggle

I guess it is too much simple but goes query result error .

I made Country table , models. I just want it query only .

The only point not usual is that I did not use 'id' field for primary field.

I used 'cc' field for primary .

That's it. but it goes response data error .

in migration file

Schema::create('countries', function (Blueprint $table) {

in Controller

function run() {
  $countries = Country::OrderBy('cc', 'asc')->take(3)->get();
  $data['countries'] = $countries;
  return response()->json( $data, 200);

in Country Model

class Country extends Model
    protected $table='countries';
    protected $primaryKey = 'cc';
    public $timestamps = false;
  protected $fillable = ['cc', 'name_eng'];

in result

  "countries": [
      "cc": 0, --> it should be 'ad' 
      "name_eng": "Andorra",
      "cc": 0, --> it should be 'ae' 
      "name_eng": "United Arab Emirates",
      "cc": 0, --> it should be 'af' 
      "name_eng": "Afghanistan",

in backup sql

INSERT INTO `countries` (`cc`, `name_eng`)
    ('ad', 'Andorra'),
    ('ae', 'United Arab Emirates'),
    ('af', 'Afghanistan');

Why Do I get lost 'cc' field values ?

I dont understand where the wrong comes from.

somebody can help me ?

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