Update migrations and add foreign key, update related models

This commit is contained in:
DariusIII
2019-03-12 09:17:40 +01:00
parent a822c305e0
commit bc2fd756b6
3 changed files with 41 additions and 2 deletions
+1 -1
View File
@@ -264,7 +264,7 @@ class Category extends Model
*/
public function parent()
{
return $this->belongsTo(static::class, 'parentid');
return $this->belongsTo(RootCategory::class, 'root_categories_id');
}
/**
+7 -1
View File
@@ -6,5 +6,11 @@ use Illuminate\Database\Eloquent\Model;
class RootCategory extends Model
{
//
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function categories()
{
return $this->hasMany(Category::class, 'root_categories_id');
}
}
@@ -0,0 +1,33 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('categories', function (Blueprint $table) {
$table->renameColumn('parentid', 'root_categories_id');
$table->foreign(['root_categories_id'], 'fk_root_categories_id')->references('id')->on('root_categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('categories', function (Blueprint $table) {
//
});
}
}