【发布时间】:2017-04-07 01:09:32
【问题描述】:
我是 Laravel 的新手。 这是我的架构:
Schema::create('cluster_info', function (Blueprint $table) {
$table->increments('id');
$table->string('code')->unique();
$table->string('pa_lastname');
$table->string('pa_firstname');
$table->string('pa_middlename');
$table->string('pa_suffix');
$table->integer('branch_id');
$table->timestamps();
});
Schema::create('cluster_grouping', function (Blueprint $table) {
$table->increments('id');
$table->integer('cluster_id');
$table->integer('client_id');
$table->timestamps();
});
Schema::create('branch', function (Blueprint $table) {
$table->increments('id');
$table->string('code')->unique();
$table->string('name');
$table->string('region');
$table->timestamps();
});
我想加入cluster_info 到branch。所以在我的Cluster_Info 模型中:
public function Branches(){
return $this->hasOne('App\Branch');
}
当我在我的控件中调用它时,它具有 null 值
$cluster = new App\Cluster_Info;
dd($cluster->Branches());
【问题讨论】:
-
cluster_infoinbranch的外键是什么? -
在cluster_info表的分支表中定义外键
-
@BalrajAllam 如何在 clusteri_info 中定义分支表的外键?
-
使用
php artisan migrate:rollback回滚迁移,然后在分支架构中添加$table->integer('cluster_info_id')$table->foreign('cluster_info_id')->references('id')->on('cluster_info')。
标签: php laravel laravel-5 model eloquent