タグ: Laravel
学んだこと
- xxx_id を持つテーブルモデルから xxx テーブルのモデルを取得するには、 belongsTo メソッドでリレーションメソッドを定義する。 -> 1対多 (Inverse) Eloquent:リレーション 5.8 Laravel
- リレーションメソッドよりも動的プロパティを使う。 -> リレーションメソッド 対 動的プロパティ Eloquent:リレーション 5.8 Laravel
- コレクション要素を指定した文字列を末尾に付けて連結する -> implode() コレクション 5.8 Laravel
Laravel 5.8 における belongsToMany の引数まとめ
- 第 1 引数では最終的な接続先モデル名を名前空間含めて指定する
- 第 2 引数では中間テーブル名を指定する
- 第 3 引数では接続元モデル ID を示す中間テーブル内のカラム名を指定する
- 第 4 引数では最終的な接続先モデル ID を示す中間テーブル内のカラム名を指定する
- 第 5 引数では接続元モデル ID のカラム名を指定する
- 第 4 引数では最終的な接続先モデル ID のカラム名を指定する
学んだこと
- aaa テーブルに xxx_id カラムがある場合、 xxx を取得するには belongsTo() を使う。 aaa モデルクラスで次のように使う。なお、 xxx テーブルのプライマーキーは id とする。
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Aaa extends Model
{
public function xxx(): BelongsTo
{
return $this->belongsTo('App¥Xxx', 'xxx_id', 'id');
}
}
- リレーションへはメソッドとしてもアクセスできるが、プロパティとしてアクセスでき、そのほうが性能が有利になる。なぜなら、実際にアクセスされた時にだけそのリレーションのデータはロードされる (遅延ロード) ため。
- Eagerロード で N + 1 問題を回避できる。今回の例では、投稿一覧の取得で 1 回、取得できた 5 件の投稿の各ユーザを取得するのにさらに 5 回、 SQL が発行されていた。それを、 Eager ロードすることによって、 2 回の SQL 発行へ減らすことができた。
学んだこと、気をつけたことまとめ
- 既存の属性を元に算出した、新しい値をアクセサを使用し返す
- フロント側で投稿ステータスの値を判定して対応する日本語を表示、というようなことは絶対に行わない。ビューにロジックが入り込んでしまい読みづらく複雑になってしまうし、同じ内容を必要な回数分コピペしなければならなくなるため。
- 投稿ステータスの日本語名は、定数ファイル (例えば config/const.php) に入れるのではなく、投稿モデル (app/Models/Wp/PostType/Post.php) に持たせた。投稿ステータスを調べたい、加工修正したい時に最も知っているべきで存在しても違和感のないしっくりくる場所が投稿モデルなため。
やりたいこと
- フロント側に投稿 (Post) の投稿ステータス (post_status) の日本語を表示したい。
やりたいこと
- WordPress の データベース構造をそのまま使って、管理者の投稿一覧ページを作りたい。 Laravel 5.5 を使いたい。
学んだことまとめ
WordPress
Laravel 5.5
まとめ
- Laravel 5.5 の package.json には、 "bootstrap-sass": "^3.3.7" とあり、 Bootstrap のバージョンが 3 である。これを 4 にした。
- bootstrap-sass のバージョン 4 はなく、 単純な Bootstrap 4 を指定することからか、 package.json 以外の次のファイルの修正が必要となった。
- resources/assets/js/bootstrap.js
- resources/assets/sass/_variables.scss
- resources/assets/sass/app.scss
Laravel 5.5 の妥協できる PHP-CS-Fixer 設定ファイルが完成した – oki2a24 を作る途中で、 https://styleci.readme.io/docs/presets#section-laravel の各ルールが、 PHP-CS-Fixer のどのルールと対応するのか、それは PHP-CS-Fixer の @PSR ルールや @PhpCsFixer ルールに含まれるか、の対応が知りたいと思い、一覧表にまとめました。
妥協ライン
- Laravel 5.5 本体に PHP-CS-Fixer を適用しても何も修正されない。
- 作成直後の Laravel 5.5 に PHP-CS-Fixer を適用しても何も修正されない。
- https://laravel.com/docs/5.8/contributions#coding-style に従う。しかし、 PHP-CS-Fixer によって本体や作成直後のプロジェクトが修正されてしまう場合は、最低限修正されないように最低限の変更を加える。
- https://styleci.readme.io/docs/presets#section-laravel に従う。しかし、 PHP-CS-Fixer によって本体や作成直後のプロジェクトが修正されてしまう場合は、最低限修正されないように最低限の変更を加える。
Laravel 5.5 の StyleCI 設定を PHP-CS-Fixer で表現しようとしたもの
.php_cs.dist
<?php
/*
* This document has been generated with
* https://mlocati.github.io/php-cs-fixer-configurator/?version=2.15#configurator
* you can change this configuration by importing this file.
*/
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'align_multiline_comment' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => true,
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => ['statements' => ['return']],
'braces' => true,
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['method']],
'class_definition' => true,
'concat_space' => true,
'declare_equal_normalize' => true,
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
'function_declaration' => true,
'function_typehint_space' => true,
'hash_to_slash_comment' => true,
'heredoc_to_nowdoc' => true,
'include' => true,
'increment_style' => ['style' => 'post'],
'indentation_type' => true,
'lowercase_cast' => true,
'lowercase_constants' => true,
'lowercase_keywords' => true,
'magic_constant_casing' => true,
'method_argument_space' => true,
'multiline_whitespace_before_semicolons' => true,
'no_alias_functions' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => ['tokens' => ['extra', 'throw', 'use']],
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_around_offset' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unreachable_default_argument_value' => true,
'no_unused_imports' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'not_operator_with_successor_space' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => ['sort_algorithm' => 'length'],
'phpdoc_indent' => true,
'phpdoc_inline_tag' => true,
'phpdoc_no_access' => true,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'psr4' => true,
'self_accessor' => true,
'short_scalar_cast' => true,
'simplified_null_return' => true,
'single_blank_line_at_eof' => true,
'single_blank_line_before_namespace' => true,
'single_class_element_per_statement' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'switch_case_semicolon_to_colon' => true,
'switch_case_space' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => true,
'whitespace_after_comma_in_array' => true,
])
->setFinder(PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
)
;
