Как Laravel определяет имя внешнего ключа по умолчанию в файле модели? - PullRequest
0 голосов
/ 07 апреля 2019

Я новичок в Laravel, и мне интересно, как на самом деле Laravel определяет имя внешнего ключа по умолчанию.

Согласно документу Laravel, в нем говорится: "Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id".

Тогда, в этом случае↓, внешний ключ по умолчанию должен называться info_id, как сказано в документе.

app/Models/Item.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
   public function itemInfo()
   {
       return $this->hasOne(‘App\Models\ItemInfo’);
   }
}

Я просто изменил имя функции info на infooo ТО, ошибки не произошло.Это работает ... Я ожидал, что произошла ошибка, потому что я думал, что Laravel назовет внешний ключ по умолчанию как infooo_id, а в таблицах нет такого имени столбца.
Это означает, что Laravel не определяет значение по умолчаниюимя внешнего ключа по имени метода, не так ли?

Тогда из какой части на самом деле Laravel определяет внешний ключ по умолчанию из?

Это файлы миграции таблиц:

database/YYYY_MM_dd_hhiiss_create_items_table.php

<?php

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

class CreateItemsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create(‘items’, function (Blueprint $table) {
           $table->increments(‘id’);
           $table->string(‘name’);
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists(‘items’);
   }
}

YYYY_mm_dd_hhiiss_create_item_infos_table.php

<?php

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

class CreateItemInfosTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create(‘item_infos’, function (Blueprint $table) {
           $table->increments(‘id’);
           $table->integer(‘item_id’)->unsigned();
           $table->string(‘genre’);
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists(‘item_infos’);
   }
}
...