Why do my models return 0 when accessing a string-based primary key model property in Laravel 4?

StackOverflow https://stackoverflow.com/questions/22155444

  •  19-10-2022
  •  | 
  •  

Question

I have a table set up something like this:

Schema::create('capability', function($T) {
    $T->string('code',32)->primary();
    $T->string('name',64);
});

And a corresponding model like this:

class Capability extends BaseModel {
    protected $table = 'capability';
    protected $primaryKey = 'code';
    public $timestamps = false;
}

When I seed some data like this...

$c1 = Capability::create(array(
    'name'  => 'Manage Clients',
    'code'  => 'manage_clients'
));

...and access the data members of $c1 like this...

echo $c1->name.", ".$c1->code;

...the result is Manage Clients, 0.

Why is the result not Manage Clients, manage_clients as I was expecting?

Was it helpful?

Solution

I needed to add public $incrementing = false to the model class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top