Vinoth Kumar
Vinoth Kumar

Reputation: 423

How to create a new row based on custom value ID in laravel 4.2

Am having a table with id as primary key not auto increment, I need to push the custom value as an ID. I tried with firstOrNew() and findOrCreate() and create() by pushing the ID as an argument like

$var_id = '99';
$var = Table::findOrCreate(['id'=>$var_id]);
return $var;

But its creating new row with 0 as ID. Can any one please suggest me how to create ID with 99.

Thanks

Upvotes: 0

Views: 468

Answers (3)

Robert
Robert

Reputation: 5963

I guess you're code has a small typo, in your case you probably want to use firstOrCreate or findOrNew.

Either way, by default all model attributes are guarded. Add id to your model fillable array, like so:

protected $fillable = ['id']; // perhaps more ?

And try again.

Upvotes: 0

Victor
Victor

Reputation: 556

Did you create your table manually or through a migration? Make sure your id is set to "auto-increment" on your table. If you create through a migration it would look something like this $table->increments('id');

Upvotes: 1

Harigovind R
Harigovind R

Reputation: 826

Try laravel query builder its flexible to use.

DB::table('tablename')->insert( array('id' => '99'));

For more help check this link out Laravel 4.2 query builder

Upvotes: 0

Related Questions