Laravel 5.2 Eloquent 与不规则名称的关系
Laravel 5.2 Eloquent Relationships with Irregular Names
我正在 Laravel 中构建我的第一个项目,并且 运行 遇到了两个 table 之间一对多关系的问题。
从历史上看,我会在 SQL 中做这样的事情来实现我的最终目标:
SELECT tag_key.key
FROM tag
LEFT JOIN tag_key
ON tag.tag_key_id = tag_key.id;
对于 Laravel,我正在尝试以 ORM 方式做事,但我被挂断了,可能是在某个命名问题上。这是代码:
第 1 部分:迁移:
"tag_keys" table
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagKeysTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tag_keys', function (Blueprint $table) {
$table->increments('id');
$table->string('key', 128);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tag_keys');
}
}
"tags" table
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('value', 128);
$table->integer('tag_key_id')->unsigned()->index();
$table->foreign('tag_key_id')->references('id')->on('tag_keys')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tags');
}
}
第 2 部分:型号:
"TagKey" 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TagKey extends Model
{
protected $fillable = [
'key'
];
protected $dates = [];
protected $table = 'tag_keys';
/**
* Tag Keys have many Tags
*/
public function values()
{
return $this->hasMany('App\Tag');
}
}
"Tag" 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = [
'value',
'tag_key_id'
];
protected $dates = [];
/**
* Tag values belong to Tag Keys
*/
public function key()
{
return $this->belongsTo('App\TagKey');
}
}
独立地,它们都工作得很好。但是,当我跳进 tinker 并尝试这个时(假设 "tag" 和 "tag_key" table 中都有一个有效行,并且 "tag" 中的 id 1 有"tag_key_id"列下"tag_key"table中1的值):
$tag = App\Tag::first();
$tag->key;
=> null
我在这里错过了什么?我如何建立这个协会?
当外键名称不遵循Eloquent约定时("snake case"拥有模型的名称并以_id作为后缀),您应该在关系中指定它:
TagKey 对象:
return $this->hasMany('App\Tag', 'tag_key_id');
关键对象:
return $this->belongsTo('App\TagKey', 'tag_key_id');
我正在 Laravel 中构建我的第一个项目,并且 运行 遇到了两个 table 之间一对多关系的问题。
从历史上看,我会在 SQL 中做这样的事情来实现我的最终目标:
SELECT tag_key.key
FROM tag
LEFT JOIN tag_key
ON tag.tag_key_id = tag_key.id;
对于 Laravel,我正在尝试以 ORM 方式做事,但我被挂断了,可能是在某个命名问题上。这是代码:
第 1 部分:迁移:
"tag_keys" table
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagKeysTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tag_keys', function (Blueprint $table) {
$table->increments('id');
$table->string('key', 128);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tag_keys');
}
}
"tags" table
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('value', 128);
$table->integer('tag_key_id')->unsigned()->index();
$table->foreign('tag_key_id')->references('id')->on('tag_keys')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tags');
}
}
第 2 部分:型号:
"TagKey" 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TagKey extends Model
{
protected $fillable = [
'key'
];
protected $dates = [];
protected $table = 'tag_keys';
/**
* Tag Keys have many Tags
*/
public function values()
{
return $this->hasMany('App\Tag');
}
}
"Tag" 型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = [
'value',
'tag_key_id'
];
protected $dates = [];
/**
* Tag values belong to Tag Keys
*/
public function key()
{
return $this->belongsTo('App\TagKey');
}
}
独立地,它们都工作得很好。但是,当我跳进 tinker 并尝试这个时(假设 "tag" 和 "tag_key" table 中都有一个有效行,并且 "tag" 中的 id 1 有"tag_key_id"列下"tag_key"table中1的值):
$tag = App\Tag::first();
$tag->key;
=> null
我在这里错过了什么?我如何建立这个协会?
当外键名称不遵循Eloquent约定时("snake case"拥有模型的名称并以_id作为后缀),您应该在关系中指定它:
TagKey 对象:
return $this->hasMany('App\Tag', 'tag_key_id');
关键对象:
return $this->belongsTo('App\TagKey', 'tag_key_id');