다대다(N:N) 관계
예를 들어, 한 게시물에 검색 요소로서의 태그를 설정할 때, 어느 한 게시물에 있는 태그가 다른 게시물에도 있을 수 있다. 이러한 관계를 다대다 관계라는 용어를 사용한다.이러한 관계는 database에서 주로 3개의 테이블로 관계를 설정하여 설명 할 수 있다.
1 2 3 4 |
// 태그 테이블 $ php artisan make:migration create_tags_table --create=tags // 태그와 게시글의 연결 $ php artisan make:migration create_board_tag_table --create=board_tag |
경로
database/migrations/TIMESTAMP_create_tags_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class CreateTagsTable extends Migration { public function up() { Schema::create('tags', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('slug')->index(); $table->timestamps(); }); } public function down() { Schema::drop('tags'); } } |
경로
database/migrations/TIMESTAMP_create_board_tag_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class CreateBoardTagTable extends Migration { public function up() { Schema::create('board_tag', function (Blueprint $table) { $table->increments('id'); $table->integer('board_id')->unsigned(); $table->integer('tag_id')->unsigned(); $table->foreign('board_id')->references('id')->on('boards')->onDelete('cascade'); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); } public function down() { Schema::drop('board_tag'); } } |
1 |
$ php artisan migrate |
관계설정
1 |
$ php artisan make:model Tag |
경로
app/Tag.php
1 2 3 4 5 6 7 8 9 |
class Tag extends Model { protected $fillable = ['name', 'slug']; public function boards() { return $this->belongsToMany(Board::class); } } |
경로
app/Board.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Board extends Model { protected $fillable = ['title', 'content']; public function user() { return $this->belongsTo(User::class); } public function tags() { return $this->belongsToMany(Tag::class); } } |
팅커 링
boards
테이블에 2개의 게시물과 tags
에 두개의 태그를 추가하여 이를 관계를 설정한 대로 출력이 되는지 확인 해 보도록 하겠습니다.
1 |
$ php artisan tinker |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Psy Shell v0.8.18 (PHP 7.2.4 — cli) by Justin Hileman >>> App\User::find(1)->boards()->create(['title' => 'First board','content' => 'content']); >>> App\User::find(1)->boards()->create(['title' => 'Sec board','content' => 'content2']); >>> App\Tag::create(['name' => '태그1','slug' => 'tag1']); >>> App\Tag::create(['name' => '태그1','slug' => 'tag1']); >>> App\Tag::all(); => Illuminate\Database\Eloquent\Collection {#747 all: [ App\Tag {#746 id: 1, name: "태그1", slug: "tag1", created_at: "2018-04-29 08:29:43", updated_at: "2018-04-29 08:29:43", }, App\Tag {#744 id: 2, name: "태그2", slug: "tag2", created_at: "2018-04-29 08:30:01", updated_at: "2018-04-29 08:30:01", }, ], } |
다대다 테스트
1 2 3 |
>>> $board = App\Board::find(1); >>> $board->tags()->sync([1,2]); >>> $board->tags->pluck('name', 'id'); |
board_tag 테이블에 위와 같이 관계가 맺어 지는 것을 확인 할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
App\Tag::find(1)->boards; => Illuminate\Database\Eloquent\Collection {#775 all: [ App\Board {#776 id: 1, user_id: 1, title: "First board", content: "content", created_at: "2018-04-29 07:55:15", updated_at: "2018-04-29 07:55:15", pivot: Illuminate\Database\Eloquent\Relations\Pivot {#772 tag_id: 1, board_id: 1, }, }, ], } >>> |
다형적 관계에 대해서는 공식 문서를 찾아 보시기 바랍니다.
PHP 라라벨 5 On Mac 포스팅
[PHP 라라벨 5 On Mac] 1 : 다루게 될 내용들
[PHP 라라벨 5 On Mac] 2 : Composer, Valet, Framework
[PHP 라라벨 5 On Mac] 3 : Framework
[PHP 라라벨 5 On Mac] 4 : Routing
[PHP 라라벨 5 On Mac] 5 : 블레이드 템플릿 1 (View)
[PHP 라라벨 5 On Mac] 6 : 블레이드 템플릿 2 (View)
[PHP 라라벨 5 On Mac] 7 : 블레이드 템플릿 3 (View)
[PHP 라라벨 5 On Mac] 8 : 마이그레이션 (Create Table)
[PHP 라라벨 5 On Mac] 9 : 마이그레이션 (컬럼 추가 및 데이타 스토리 엔진 설정)
[PHP 라라벨 5 On Mac] 10 : 마이그레이션 (적용)
[PHP 라라벨 5 On Mac] 11 : 마이그레이션 (컬럼 수정)
[PHP 라라벨 5 On Mac] 12 : Laravel Tinker Shell
[PHP 라라벨 5 On Mac] 13 : Laravel Tinker Shell (쿼리 빌더)
[PHP 라라벨 5 On Mac] 14 : 엘로퀀트 ORM 1
[PHP 라라벨 5 On Mac] 15 : 엘로퀀트 ORM 2
[PHP 라라벨 5 On Mac] 16 : 엘로퀀트 ORM 3
Leave a Reply
Want to join the discussion?Feel free to contribute!