How do I make an id / primary key column that's different from "id"? When I try it it creates an error.
class CreateEmployeesTable extends Migration
{
// ...
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id('employee_id');
$table->string('first_name', 100);
$table->string('last_name', 100);
$table->string('email', 200);
$table->timestamps();
});
}
// ...
}
Trying it out:
php artisan tinker
Psy Shell v0.10.8 (PHP 7.3.20 — cli) by Justin Hileman
>>> $employee = new App\Models\Employee
=> App\Models\Employee {#3452}
// ...
>>> $employee->save();
<warning>PHP Warning: oci_execute(): ORA-00904: "ID": invalid identifier in <path>Statement.php on line 159</warning>
Yajra\Pdo\Oci8\Exceptions\Oci8Exception with message 'Error Code : 904
Error Message : ORA-00904: "ID": invalid identifier
Position : 132
Statement : insert into "EMPLOYEES" ("FIRST_NAME", "LAST_NAME", "EMAIL", "UPDATED_AT", "CREATED_AT") values (:p0, :p1, :p2, :p3, :p4) returning "ID" into :p5
Bindings : [Roger,Rabbit,[email protected],2021-11-04 22:13:56,2021-11-04 22:13:56,0]
CodePudding user response:
When you use a primary key in a table other than id
you need to specify it in the model class with
class Employee extends Model
{
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'employee_id';
}
CodePudding user response:
id()
The id method is an alias of the bigIncrements method. By default, the method will create an id column; $table->id();
bigIncrements()
The bigIncrements method creates an auto-incrementing UNSIGNED BIGINT (primary key) equivalent column:
$table->bigIncrements('employee_id');