I ran into a problem when my Laravel Eloquent Model does not give me the value of a column named 'id', it just turns into an integer (0) instead of a string. I, although the column was somehow protected, but in other models, where "id" is intenger, it returns the value just fine.
Question: I can not use VARCHAR for primary keys?
Note. I need to create more tables with matching models where the primary key should be a row; I'm new to Laravel
Migration:
Schema::create('country', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->string('id', 5); $table->string('name', 45); $table->string('currency_symbol', 5); $table->string('currency_name', 45); $table->float('tax_rate'); $table->string('url')->nullable(); $table->string('support_email', 90); $table->string('noreply_email', 90); $table->boolean('active'); $table->boolean('root'); $table->primary('id'); });
The model is really simple:
use Illuminate\Database\Eloquent\Model; class Country extends Model { protected $table = 'country'; }
But when I try to get it ...
echo Country::find('ve')->first()->toJSON(); //Output: {"id":0,"name":"Test","currency_symbol":"T"..... // And... var_dump(Country::find('ve')->first()->id); //Output: int:0
But if I use the print_r () function, this will be the output:
App\Http\Models\Country\Country Object ( [table:protected] => country [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => ve [name] => Test [currency_symbol] => T [currency_name] => Test currency [tax_rate] => 12 [url] => [support_email] => support@example.com [noreply_email] => roreply@example.com [active] => 1 [root] => 1 ) [original:protected] => Array ( [id] => ve [name] => Test [currency_symbol] => T [currency_name] => Test currency [tax_rate] => 12 [url] => [support_email] => support@example.com [noreply_email] => roreply@example.com [active] => 1 [root] => 1 ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => )
if necessary:
- Laravel Version: 5.2
- PHP: 5.6.15