Benim Laravel'in Eloquent Modelinin bana 'id' adı verilen sütunun değerini vermediği bir sorunla karşılaştım, sadece bir tamsayı (0) yerine tamsayı (0) dize. Her ne kadar sütun bir şekilde korunmuş olsa da, 'id' in bir intikam olduğu diğer modellerde de değeri iyi para cezasına çevirir.Birincil anahtar varchar olduğunda Laravel'in Eloquent sütun değerini geri alma
Soru: Birincil anahtarlar için VARCHAR'ı kullanamıyorum?
Not: Birincil anahtarın bir dize olması gereken ilgili modellerle daha fazla tablo oluşturmam gerekir; Ben laravel tür yeniyim
Göç:
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');
});
Modeli gerçekten basit: i geri almak için çalışırken ...
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $table = 'country';
}
Ama
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
Ancak print_r() işlevini kullanırsam, bu çıktı:
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] =>
)
gerekirse:
- laravel sürümü: 5.2
- PHP: 5.6.15
Çok teşekkür ederim. Doğru ... birincil anahtarla bulun. Laravel için çayırıyım ... ama oraya varıyorum! : c –
Çözüm için teşekkür ederiz! Yine de, bu, çalışmanın doğru olması için değil, sezgisel değil, çerçeve hakkında bilmeniz gereken şeylerden biridir. Sütun sayısal değilse, neden tamsayıya dönüştürülmelidir? –
Sadece Laravel şeyler ¯ \\ _ (ツ) _/¯ –