<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTaggablesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('taggables', function (Blueprint $table) {
$table->bigInteger('tag_id');
$table->bigInteger('taggable_id');
$table->string('taggable_type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('taggables');
}
}
C:\xampp\htdocs\hanam.com\app\Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
protected $fillable = ['user_id','title'];
public function user() {
return $this->belongsTo(User::class)->withDefault([
'name' => 'Guest User'
]);
}
// public function tags() {
// // return $this->belongsToMany(Tag::class,'post_tag','post_id','tag_id');
// // return $this->belongsToMany(Tag::class)->withTimestamps();
// return $this->belongsToMany(Tag::class)
// ->using(PostTag::class)
// ->withTimestamps()
// ->withPivot('status');
// }
public function comments() {
return $this->morphMany(Comment::class,'commentable');
}
public function comment() {
// return $this->morphOne(Comment::class,'commentable');
return $this->morphOne(Comment::class,'commentable')->latest();
}
public function tags() {
return $this->morphToMany(Tag::class,'taggable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
protected $fillable = ['user_id','title'];
public function user() {
return $this->belongsTo(User::class)->withDefault([
'name' => 'Guest User'
]);
}
// public function tags() {
// // return $this->belongsToMany(Tag::class,'post_tag','post_id','tag_id');
// // return $this->belongsToMany(Tag::class)->withTimestamps();
// return $this->belongsToMany(Tag::class)
// ->using(PostTag::class)
// ->withTimestamps()
// ->withPivot('status');
// }
public function comments() {
return $this->morphMany(Comment::class,'commentable');
}
public function comment() {
// return $this->morphOne(Comment::class,'commentable');
return $this->morphOne(Comment::class,'commentable')->latest();
}
public function tags() {
return $this->morphToMany(Tag::class,'taggable');
}
}
C:\xampp\htdocs\hanam.com\app\Tag.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
protected $fillable = [
'name',
];
// public function posts() {
// return $this->belongsToMany(Post::class, 'post_tag', 'tag_id', 'post_id');
// }
public function posts() {
return $this->morphedByMany(Post::class, 'taggable');
}
}
C:\xampp\htdocs\hanam.com\app\Video.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model {
protected $guarded = [];
public function comments() {
return $this->morphMany(Comment::class,'commentable');
}
public function comment() {
return $this->morphOne(Comment::class,'commentable');
}
public function tags() {
return $this->morphToMany(Tag::class,'taggable');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTaggablesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('taggables', function (Blueprint $table) {
$table->bigInteger('tag_id');
$table->bigInteger('taggable_id');
$table->string('taggable_type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('taggables');
}
}