【问题标题】:laravel making categories for a post (normalizing table)laravel 为帖子制作类别(规范化表)
【发布时间】:2015-01-17 03:53:17
【问题描述】:

数据库中的 Laravel 规范化关系。

所以我有一个包含工作的工作表。以及包含类别的类别表。

工作可以有多个类别。

有没有一种简单的方法可以使关系正常化?

Schema::create('jobs', function($table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('slug');
            $table->string('title');
            $table->string('excerpt')->nullable();
            $table->text('content');
            $table->integer('delivery');
            $table->integer('price');
            $table->unique(array('user_id', 'slug'));
            $table->timestamps();
        });

Schema::create('categories', function(Blueprint $table)
    {
      // These columns are needed for Baum's Nested Set implementation to work.
      // Column names may be changed, but they *must* all exist and be modified
      // in the model.
      // Take a look at the model scaffold comments for details.
      // We add indexes on parent_id, lft, rgt columns by default.
      $table->increments('id');
      $table->integer('parent_id')->nullable()->index();
      $table->integer('lft')->nullable()->index();
      $table->integer('rgt')->nullable()->index();
      $table->integer('depth')->nullable();

      // Add additional columns here (f.ex: name, slug, path, etc.)
      $table->string('name')->unique();
      $table->string('slug')->unique();
      $table->string('description')->nullable();
    });

我的第一反应是创建一个保存关系的中间表:

Schema::create('jobs_categories', function($table)
        {
            $table->increments('id');
            $table->integer('job_id')->unsigned();
            $table->integer('category_id')->unsigned();
            $table->unique(array('job_id', 'category_id'));
        });

但我不知道该怎么做,如果我想获取所有 $jobs 的类别,我该怎么办?

如果我想获得 $job 类别该怎么办?

hasOne、hasMany 更适合这个吗?

【问题讨论】:

  • 视情况而定。一个工作可以有多个类别。同一个类别可以属于多个职位吗?
  • @user3158900 是的,为什么不呢?帖子有很多分类,分类有很多帖子。
  • lukasgeiter 有一个很好的答案,所以我不会费心创建另一个,但我要补充的唯一一点是 Laravel 期望数据透视表是单数并按字母顺序排列。只需将jobs_categories 更改为category_job
  • @user3158900 你看过我的回答了吗? (数据透视表部分的第一句话);)

标签: php laravel


【解决方案1】:

您所描述的是多对多关系。是的,需要像jobs_categories 这样的数据透视表。以下是遵循 Laravel 命名约定并利用关系的方法:

数据透视表

jobs_categories 很好,但 Laravel 喜欢 category_job(单数和字母顺序)(这样您就不必在关系中指定表名)

Schema::create('category_job', function($table){
    $table->increments('id');
    $table->integer('job_id')->unsigned();
    $table->integer('category_id')->unsigned();
    $table->unique(array('job_id', 'category_id'));

    // foreign key constraints are optional (but pretty useful, especially with cascade delete
    $table->foreign('job_id')->references('id')->on('jobs')->onDelete('cascade');
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});

关系

工作模式

public function categories(){
    return $this->belongsToMany('Category');
}

类别模型

public function jobs(){
    return $this->belongsToMany('Job');
}

用法

类别急切加载的作业

$jobs = Job::with('categories')->get();

访问工作类别

$job = Job::find(1);
$categories = $job->categories;

Visit the Laravel docs for more information on Eloquent relationships

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 2016-12-18
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多