Eloquent model returns 0 as primary key
Ad
I have this migration:
Schema::create('provincias', function (Blueprint $table) {
$table->string('codigo', 2);
$table->string('nombre', 50);
$table->timestamp('creado');
$table->primary('codigo');
});
This is my model:
class Provincia extends Model
{
protected $primaryKey = 'codigo';
public $timestamps = false;
}
I run the migration and save data like this:
$provincia = new Provincia;
$provincia->codigo = "BA";
$provincia->nombre = "Buenos Aires";
$provincia->save();
The problem is that when I get all and dump:
$provincias = Provincia::all();
return $provincias;
The primary key "codigo" is always 0 even when I check the database table and it has the proper value:
{
codigo: 0,
nombre: "Buenos Aires",
creado: "2015-12-24 21:00:24"
}
Ad
Answer
Ad
You can try setting public $incrementing = false;
on your model so eloquent doesn't expect your primary key to be an autoincrement primary key.
Ad
source: stackoverflow.com
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad