Ho riscontrato un problema in cui il mio modello Eloquent di Laravel non mi fornisce il valore della colonna denominata "id", ma diventa un intero (0) anziché un stringa. Ho pensato che la colonna fosse in qualche modo protetta ma in altri modelli dove l''id' è un intendente restituisce il valore alla perfezione.Impossibile recuperare il valore di colonna da Eloquent di Laravel quando la chiave primaria è varchar
Domanda: Non posso utilizzare VARCHAR per le chiavi primarie?
Nota: Devo creare più tabelle con i rispettivi modelli in cui la chiave primaria deve essere una stringa; Sono un po 'nuova laravel
Migrazione:
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');
});
modello è molto semplice:
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $table = 'country';
}
ma quando provo a recuperarlo ...
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
Ma se uso la funzione print_r() questo è l'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] => [email protected]
[noreply_email] => [email protected]
[active] => 1
[root] => 1
)
[original:protected] => Array
(
[id] => ve
[name] => Test
[currency_symbol] => T
[currency_name] => Test currency
[tax_rate] => 12
[url] =>
[support_email] => [email protected]
[noreply_email] => [email protected]
[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] =>
)
se necessario:
- laravel versione: 5.2
- PHP: 5.6.15
Grazie mille. Giusto ... find() per chiave primaria. Sono un debuttante per Laravel ... ma sto arrivando! : c –
Grazie per la soluzione! Tuttavia, questa è una delle cose che devi sapere sul framework per farlo funzionare correttamente, non in modo intuitivo. Se la colonna non è numerica, perché dovrebbe essere convertita in numero intero !? –
Solo cose di Laravel ¯ \\ _ (ツ) _/¯ –