duan33360 2015-01-16 19:41
浏览 20
已采纳

laravel为帖子制作类别(规范化表)

Laravel normalizing relationship in DB.

So I've jobs table that contains job. And categories table that contains category.

job can have multiple categories.

Is there a laravely way of normalizing the relationship?

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();
    });

My first instinct is to create an intermediary table that holds relationship:

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'));
        });

But I'm not sure how to proceede, what would I do if I want to get categories along with all $jobs?

What do I do if I want to get $job category?

Is hasOne, hasMany a better suited for this?

  • 写回答

1条回答 默认 最新

  • dongyun7571 2015-01-16 19:53
    关注

    What you're describing is a many-to-many relationship. And yes, a pivot table like jobs_categories is needed. Here's how you do it following Laravels naming convention and making use of relationships:

    Pivot table

    jobs_categories is fine but Laravel likes category_job (singular and alphabetical order) (This way you don't have to specify the table name in your relation)

    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');
    });
    

    Relationships

    Job model

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

    Category model

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

    Usage

    Jobs with categories eager loaded

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

    Access categories of a job

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

    Visit the Laravel docs for more information on Eloquent relationships

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图