Created
April 4, 2018 12:20
-
-
Save iboved/be21e72d329544a5553f9714d09bec7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Demo\Models; | |
use Model; | |
use October\Rain\Database\Pivot; | |
use Backend\Facades\BackendAuth; | |
class CategorizablePivot extends Pivot | |
{ | |
public function beforeCreate() | |
{ | |
$this->created_by = BackendAuth::getUser()->id; | |
$this->updated_by = BackendAuth::getUser()->id; | |
} | |
public function beforeUpdate() | |
{ | |
$this->updated_by = BackendAuth::getUser()->id; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Demo\Models; | |
use Model; | |
class Category extends Model | |
{ | |
/** | |
* @var string The database table used by the model. | |
*/ | |
public $table = 'acme_demo_categories'; | |
/** | |
* @var array Guarded fields | |
*/ | |
protected $guarded = ['*']; | |
/** | |
* @var array Fillable fields | |
*/ | |
protected $fillable = []; | |
public $morphedByMany = [ | |
'posts' => ['Acme\Demo\Models\Post', 'name' => 'categorizable'], | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Demo\Updates; | |
use Schema; | |
use October\Rain\Database\Schema\Blueprint; | |
use October\Rain\Database\Updates\Migration; | |
class CreateCategorizablesTable extends Migration | |
{ | |
public function up() | |
{ | |
Schema::create('categorizables', function(Blueprint $table) { | |
$table->engine = 'InnoDB'; | |
$table->increments('id'); | |
$table->integer('category_id')->unsigned(); | |
$table->integer('categorizable_id')->unsigned(); | |
$table->string('categorizable_type'); | |
$table->integer('created_by')->unsigned()->nullable(); | |
$table->integer('updated_by')->unsigned()->nullable(); | |
$table->timestamp('created_at')->nullable(); | |
$table->timestamp('updated_at')->nullable(); | |
$table->foreign('created_by')->references('id')->on('backend_users')->onDelete('set null'); | |
$table->foreign('updated_by')->references('id')->on('backend_users')->onDelete('set null'); | |
$table->foreign('category_id')->references('id')->on('acme_demo_categories')->onDelete('cascade'); | |
}); | |
} | |
public function down() | |
{ | |
Schema::dropIfExists('categorizables'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Demo\Models; | |
use Model; | |
class Post extends Model | |
{ | |
/** | |
* @var string The database table used by the model. | |
*/ | |
public $table = 'acme_demo_posts'; | |
/** | |
* @var array Guarded fields | |
*/ | |
protected $guarded = ['*']; | |
/** | |
* @var array Fillable fields | |
*/ | |
protected $fillable = []; | |
public $morphToMany = [ | |
'categories' => [ | |
'Acme\Demo\Models\Category', | |
'name' => 'categorizable', | |
'pivot' => ['id', 'created_by', 'updated_by'], | |
'timestamps' => true, | |
'pivotModel' => 'Acme\Demo\Models\CategorizablePivot', | |
], | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment