LaravelからMySQLに接続する 環境情報 ・PHP 7.1.23 ・CentOS Linux release 7.4.1708 (Core) ・MySQL 5.6.39 "could not find driver"が私を襲う 背景 元々sqliteで環境を作っていたが、あまりにサーバの動作が安定しないためMySQLにすることにした。 これが悲しみの正体である 良く書かれているようなコマンドは叩いた。
はじめに はじめまして、オープンロジのエンジニアで @ttaka と申します。 この記事は OPENLOGI Advent Calendar 2017 の8日目になります。 さて、弊社サービスのサーバーサイド開発には、 PHP のフレームワークである Laravel が使われています。今回はその Laravel の ORM である Eloquent を使う際に、ちょっとハマりそうなポイントをいくつかご紹介したいと思います。 ハマりポイント 論理削除トレイトはテーブルの結合先では動作しない 論理削除自体には賛否ありますが、 Eloquent には標準でその機能が提供されています。 例えば、次のような論理削除を使用している User と Profile クラスがあるとします。 class User extends Model { use SoftDeletes; public function
PHPフレームワークのLaravelでは、default timezoneがUTCになっています。 設定を変えずにそのまま運用してしまうとアプリケーションが扱う日時がおかしくなってしまいます(ログに吐かれる日時や、DB Recordのcreated_at/updated_atなどが9時間前ずれてしまう、など)。 timezoneの変更方法 config/app.php に設定ファイルがあるじゃろ? これを ( ^ω^) ⊃) (⊂ /* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | |
Laravel firstOrCreate と firstOrNewの違いと、create、newしたか判定する方法PHPLaravel Laravelの、firstOrCreateとfirstOrNewの違いを調べました。 でも、まさしく、名前のままでした。 念のため、詳細は、 https://laravel.com/docs/5.1/eloquent firstOrCreateは、実行時にレコードを作成 その名の通り、firstか、create。 実行時に、指定したパラメータを持ったレコードが無ければ、createします。 (外部キー制約がある場合、外部キーも渡さないとcreate時にexceptionになります)
はじめに 生SQLからEloquent Modelを作りたいと思ったことはないでしょうか? 3つほど方法があるので、ご紹介します。 DBでCREATE VIEWしてそのEloquent Modelを作る 以下のように、VIEWを生成し、そのVIEWに対するEloquent Modelを作ります。 CREATE VIEW view_user_with_groups AS ( SELECT u.id, u.email, u.group_id, g.name AS group_name FROM users u LEFT OUTER JOIN groups g ON u.group_id = g.id );
Laravelっぽくリレーションシップを使うのであればEloquentのRelationshipを使うことになるでしょう。 いくつかのパターンがありますが、ここでは利用頻度が高いOne To Many(hasManyとbelongsTo)を見てみます。 また、わざわざ定義をModelに書かなくても、普通にJOINも使えますのでそれも見てみます。 その他のパターンについては、本家サイトを見て下さい。 準備:テスト用のテーブル情報 まず、テスト用のテーブルを用意します。 ここでは、別の記事で利用したテーブルを利用して、テストをしてみたいと思います。create情報などもあるので、必要な人はどうぞ。 テストに利用するテーブルの情報は以下の通りです。 概要 部署テーブル(depts)と社員テーブル(employees)が存在。 社員テーブルでは所属部署をdept_idで管理(紐付け)している。 テ
class User extends Model { public function scopeActive($query) { $query->where('active', true); } }
SELECT * FROM tbl_data WHERE (column_a = 'hoge' or column_a= 'piyo') AND (column_b= 'foo' or column_b= 'bar'); $query = TblDatum::where(function($query){ $query->orWhere('column_a', '=', 'hoge') ->orWhere('column_a', '=', 'piyo'); })->where(function($query){ $query->orWhere('column_b', '=', 'foo') ->orWhere('column_b', '=', 'bar'); }); $query = TblDatum::where(function($query) use ($hoge, $piyo) {
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く