Last Updated: January 04, 2021
·
44.28K
· rjacobsen2012

Create Laravel Eloquent model without a primary key

To create an Eloquent model for a legacy table that has no primary key, simply add the following to your model:

/**
 * primaryKey 
 * 
 * @var integer
 * @access protected
 */
protected $primaryKey = null;

/**
 * Indicates if the IDs are auto-incrementing.
 *
 * @var bool
 */
public $incrementing = false;

2 Responses
Add your response

For updating model in Laravel, don't you also have to add namespace like this
namespace App; use Illuminate\Database\Eloquent\Model; class <class name> extends Model

over 1 year ago ·

thank you.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside