Skip to content

Instantly share code, notes, and snippets.

@daijinload
Last active November 30, 2016 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daijinload/9e53da888883df470f935acdd981d97e to your computer and use it in GitHub Desktop.
Save daijinload/9e53da888883df470f935acdd981d97e to your computer and use it in GitHub Desktop.
// 参考
https://readouble.com/laravel/5.3/ja/eloquent-relationships.html
---
drop table blogs;
drop table blog_comments;
CREATE TABLE `blogs` (
`id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`zip_code` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
CREATE TABLE `blog_comments` (
`blog_id` INT(10) UNSIGNED NOT NULL,
`comment` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`zip_code` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci'
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
---
php artisan make:model Blog
php artisan make:model BlogComment
php artisan tinker
---
DB::table('blogs')->delete();
DB::table('blog_comments')->delete();
DB::table('blogs')->insert(['id' => 1, 'name' => 'name1', 'zip_code' => '111-0001']);
DB::table('blogs')->insert(['id' => 2, 'name' => 'name2', 'zip_code' => '121-0001']);
DB::table('blog_comments')->insert(['blog_id' => 1, 'comment' => 'comment11', 'zip_code' => '111-0011']);
DB::table('blog_comments')->insert(['blog_id' => 1, 'comment' => 'comment12', 'zip_code' => '111-0012']);
DB::table('blog_comments')->insert(['blog_id' => 1, 'comment' => 'comment13', 'zip_code' => '111-0013']);
DB::table('blog_comments')->insert(['blog_id' => 2, 'comment' => 'comment21', 'zip_code' => '121-0021']);
DB::table('blog_comments')->insert(['blog_id' => 2, 'comment' => 'comment22', 'zip_code' => '121-0022']);
---
// 参考
https://readouble.com/laravel/5.3/ja/eloquent-relationships.html
---
drop table blogs;
drop table blog_comments;
CREATE TABLE `blogs` (
`id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`zip_code` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
CREATE TABLE `blog_comments` (
`blog_id` INT(10) UNSIGNED NOT NULL,
`comment` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`zip_code` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci'
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
---
php artisan make:model Blog
php artisan make:model BlogComment
php artisan tinker
---
DB::table('blogs')->delete();
DB::table('blog_comments')->delete();
DB::table('blogs')->insert(['id' => 1, 'name' => 'name1', 'zip_code' => '111-0001']);
DB::table('blogs')->insert(['id' => 2, 'name' => 'name2', 'zip_code' => '121-0001']);
DB::table('blog_comments')->insert(['blog_id' => 1, 'comment' => 'comment11', 'zip_code' => '111-0011']);
DB::table('blog_comments')->insert(['blog_id' => 1, 'comment' => 'comment12', 'zip_code' => '111-0012']);
DB::table('blog_comments')->insert(['blog_id' => 1, 'comment' => 'comment13', 'zip_code' => '111-0013']);
DB::table('blog_comments')->insert(['blog_id' => 2, 'comment' => 'comment21', 'zip_code' => '121-0021']);
DB::table('blog_comments')->insert(['blog_id' => 2, 'comment' => 'comment22', 'zip_code' => '121-0022']);
DB::table('blog_comments')->insert(['blog_id' => 2, 'comment' => 'comment23', 'zip_code' => '121-0023']);
---
// blogsテーブルデータ全取得
$blogs = App\Blog::all();
foreach ($blogs as $blog) {
echo $blog->name;
}
// blog_commentsテーブルデータ全取得
$blogComments = App\BlogComment::all();
foreach ($blogComments as $blogComment) {
echo $blogComment->comment;
}
// Blogの子テーブル情報取得(hasMany)
$comments = App\Blog::find(1)->comments;
// Blogの情報に子テーブルの情報も付加して、さらにjsonにしたバージョン
$blogWithComment = App\Blog::with('comments')->find(1)->toJson();
// BlogCommentの親テーブル情報取得(belongsTo)
$blogComment = App\BlogComment::find(1);
$blogComment->blog;
// 3つ以上のブログコメントを持つblogの取得
$blog = App\Blog::has('comments', '>=', 3)->get();
// Eagerローディング無し(belongsTo)
$blogComments = App\BlogComment::all();
foreach ($blogComments as $blogComment) {
echo $blogComment->blog->name;
}
// Eagerローディング有り(belongsTo)
$blogComments = App\BlogComment::with('blog')->get();
foreach ($blogComments as $blogComment) {
echo $blogComment->blog->name;
}
---
function camelizeArrayRecursive($array)
{
$results = [];
foreach ($array AS $key => $value) {
if (is_array($value)) {
$results[Illuminate\Support\Str::camel($key)] = camelizeArrayRecursive($value);
} else {
$results[Illuminate\Support\Str::camel($key)] = $value;
}
}
return $results;
}
$arr = App\Blog::with('comments')->find(1)->toArray();
camelizeArrayRecursive($arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment