投稿/トピック/カテゴリを持つ Web サイトを作成しています:
- 投稿は複数のトピックに属することができます
- トピックは複数のカテゴリに属することができます
- もちろん、カテゴリには多くのトピックと多くの投稿があり、トピックには多くの投稿があります
これが理にかなっているといいのですが、これに最適なMYSQL構造は何ですか? 私も Laravel を使用しているので、この構造の Eloquent の関係を説明していただければ幸いです。
投稿/トピック/カテゴリを持つ Web サイトを作成しています:
これが理にかなっているといいのですが、これに最適なMYSQL構造は何ですか? 私も Laravel を使用しているので、この構造の Eloquent の関係を説明していただければ幸いです。
// Migrations
Schema::create('categorys', function($table)
{
$table->increments('id');
$table->string('name');
$table->string('description;);
$table->timestamps();
});
Schema::create('topics', function($table)
{
$table->increments('id');
$table->integer('category_id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('imageable_type');
$table->integer('imageable_id');
$table->text('content');
$table->timestamps();
})
// Models
class Category extends Eloquent {
public function topics()
{
return $this->hasMany('Topic');
}
public function posts()
{
return $this->morphMany('Post', 'imageable');
}
}
class Topic extends Eloquent {
public function category()
{
return $this->belongsTo('Category');
}
public function posts()
{
return $this->morphMany('Post', 'imageable');
}
}
class Post extends Eloquent {
public function imageable()
{
return $this->morphTo();
}
}
// Very simple controller example which walks through the relationships and echos the content.
class ConversationController extends BaseController {
public function conversations()
{
$categories = Category::all();
foreach($categories as $category)
{
foreach($category->topics as $topic)
{
foreach($topic->posts as $post)
{
echo $category->name;
echo $topic->name;
echo $post->content;
}
}
}
}
}
categorys
Laravel の標準によりよく準拠し、混乱を避けるために、わざとスペルを間違えました。