Support the ongoing development of Laravel.io →
Laravel Eloquent
0

I found a solution to override the column id to id_nameOfTable :

  1. first change the code in your migration : use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;

class CreateYourTable extends Migration

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateDepartmentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('departments', function (Blueprint $table) { $table->bigIncrements('id_departement')->primaryKey(); $table->string("name"); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::dropIfExists('departments'); } } 2)Then you need to set the $primaryKey property to the custom column name : <?php namespace App; use Illuminate\Database\Eloquent\Model; class Department extends Model { // protected $primaryKey = 'id_departement'; } 3) Finally, you run the command "php artisan serve" in the terminal.

mohamednbs, boukharilina liked this reply

2

Just wrap its in block for easy debug

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDepartmentsTable extends Migration
{
    /** * Run the migrations. * * @return void */
    public function up()
    {
        Schema::create('departments', function (Blueprint $table) {
            $table->bigIncrements('id_departement')->primaryKey();
            $table->string("name");
            $table->text('description');
            $table->timestamps();
        });
    }
    /** * Reverse the migrations. * * @return void */
    public function down()
    {
        Schema::dropIfExists('departments');
    }
}

namespace App;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
{
    protected $primaryKey = 'id_departement';
}

boukharilina liked this reply

1

Just curious as to why you want to use id_nameOfTable?

Obviously there are some benefits to just sticking with id, to name but a few...

  • Verbosity: More typing is required, and the code could look more cluttered, especially with larger table names.

  • Deviates from Laravel convention: Laravel's Eloquent ORM expects id as the primary key. If you use a different primary key, you'll need to specify it in every relevant model, which could be long winded.

  • Potentially less efficient: In some databases, smaller column names can lead to more efficient storage and faster query times, particularly when the table has a lot of rows.

However, if you have a specific need to name your primary keys differently, you can do so.

To change the default primary key in a Laravel model, you would override the $primaryKey property in your Eloquent model like so:

class User extends Model { /** * The primary key associated with the table. * * @var string */ protected $primaryKey = 'id_user'; }

boukharilina liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.