< 테이블 생성 >
게시글 : 댓글 = 1:다 관계
//=================
* 게시글 테이블 생성
CREATE TABLE `article` (
`atc_no1` int(255) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
`content` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`atc_no1`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of article
-- ----------------------------
INSERT INTO `article` VALUES (1, '글 1', '내용 1');
INSERT INTO `article` VALUES (2, '글 2', NULL);
INSERT INTO `article` VALUES (3, '글 3', NULL);
INSERT INTO `article` VALUES (4, '글 4', '내용 4');
//=================
* 댓글 테이블 생성
- 일부러 article.atc_no1 과 연결된 칼럼을 다른 이름(atc_no2)로 설정
- 외래키는 설정하지 않음, 외래키는 제약을 걸어 무결성을 높이는 역할, 실제 검색 동작등에는 없어도 무관
CREATE TABLE `comment` (
`cmt_no2` int(255) UNSIGNED NOT NULL AUTO_INCREMENT,
`atc_no2` int(255) UNSIGNED NOT NULL,
`content2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`cmt_no2`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 91 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of comment
-- ----------------------------
INSERT INTO `comment` VALUES (10, 1, '댓글 1');
INSERT INTO `comment` VALUES (20, 1, '댓글 11');
INSERT INTO `comment` VALUES (30, 3, '댓글 31');
INSERT INTO `comment` VALUES (40, 3, '댓글 32');
INSERT INTO `comment` VALUES (50, 3, '댓글 33');
INSERT INTO `comment` VALUES (60, 4, '댓글 41');
INSERT INTO `comment` VALUES (70, 4, '댓글 42');
INSERT INTO `comment` VALUES (80, 4, '댓글 43');
INSERT INTO `comment` VALUES (90, 4, '댓글 44');
//=============================================================================
< 라라벨 모델 생성 >
App\Models 폴더 생성
//============
//게시물 모델 ,
php artisan make:model Models\Article
//Article.php 수정
class Article extends Model
{
protected $table = 'article';
protected $primaryKey = 'atc_no1'; //기본키 설정을 해야 즉시 로딩이 작동한다
public function comments(){
return $this->hasMany('App\Models\Comment', 'atc_no2');
//'atc_no2'=comment테이블에 존재하는 외래키
}
}
//===========
//댓글 모델,
php artisan make:model Models\Comment
//Comment.php 수정
class Comment extends Model
{
protected $table = 'comment';
protected $primaryKey = 'cmt_no2'; //기본키 설정을 해야 즉시 로딩이 작동한다
public function article(){
return $this->belongsTo('App\Models\Article', 'atc_no2');
//'atc_no2'=comment테이블에 존재하는 외래키
}
}
//=============================================================================
< 리소스 컨트롤러 >
php artisan make:resource test\TestController
//TestController.php 수정
class TestController extends Controller
{
public function index()
{
$c1 =0;
$rets = null;
//========================
//게시글 -> 댓글
/*$atcs = Article::where('atc_no1', '>=', 2)
->with('comments') //즉시로딩
->get();
$cmts = collect();
foreach($atcs as $atc){
$cmt = $atc->comments;
if( count($cmt) == 0) continue;
$c1 += count($cmt);
$cmts->push($cmt);
}
$rets = $cmts;*/
//=================
//댓글 -> 게시글
$cmts = Comment::where('cmt_no2','>=',2)
->with('article')
->get();
$atcs = collect();
foreach($cmts as $cmt){
$atc = $cmt->article;
if( count($atc) ==0) continue;
$c1 += count($atc);
$atcs->push($atc);
}
$rets = $atcs;
//================
dump($c1);
dump($rets);
//die;
return view('test.test')->with('rets', $rets);
return 'test-index';
}
//=============================================================================
< 블레이드 >
//=====================
//resource\test\test.blade.php
<!DOCTYPE html>
<html lang='ko'>
<head>
<meta charset='utf-8'>
</head>
<body>
@section('head')
<h1>head</h1>
@stop
@section('article')
@foreach($rets as $ret)
{{--@include('test.test2');--}}
@include('test.test2');
@endforeach
@show
</body>
</html>
//=====================
//test1.blade.php - 댓글 출력
@foreach($ret as $ret2)
{{ 'comment - '.$ret2->cmt_no2.' - '.$ret2->atc_no2.' - '.$ret2->content2 }} <br>
@endforeach
//=====================
//test2.blade.php - 게시글 출력
{{ 'article - '.$ret->atc_no1.' - '.$ret->title.' - '.$ret->content }} <br>
'Code > Web' 카테고리의 다른 글
Font Awesome 사용법 (0) | 2018.12.19 |
---|---|
[라라벨] where 그룹 (블록) (0) | 2018.12.16 |
[라라벨] 즉시로딩 (Eager Loading) 사용 예제 (0) | 2018.11.20 |
라라벨 DB Eloquent ORM (0) | 2018.11.16 |
라라벨 DB 빌더 (0) | 2018.11.16 |
라라벨 컬렉션 (0) | 2018.11.16 |
댓글을 달아 주세요