LaravelからMySQLに接続する 環境情報 ・PHP 7.1.23 ・CentOS Linux release 7.4.1708 (Core) ・MySQL 5.6.39 "could not find driver"が私を襲う 背景 元々sqliteで環境を作っていたが、あまりにサーバの動作が安定しないためMySQLにすることにした。 これが悲しみの正体である 良く書かれているようなコマンドは叩いた。
PHPのフレームワークであるLaravelでは、DBアクセスを行うのにEloquentやQueryBuilderという便利なモジュールがあります。 ですが、プロジェクトによってはLaravel以外のマイクロフレームワークや、フレームワークなしの環境だけども、ORマッパーのEloquentだけは使いという状況もあります。 そこで今回は、Laravelフレームワークを使わずに、Eloquentを利用する方法を説明します。 LaravelとEloquentの関係 EloquentはLaravel向けののORマッパーライブラリとして有名ですが、Eloquent自体はLaravelと一緒に使わなくても、composerのilluminate/databaseという独立したパッケージとして提供されています。 ですので、illuminate/databaseパッケージだけを個別で取得すれば、Larave
【第4回】Composerを使ってお手軽アプリケーション開発 本連載ではComposerで公開されているパッケージの中から、フレームワークを問わず汎用的に使えるライブラリをサンプルコードと共に紹介します。 今回はDBを扱う「Eloquent(Illuminate/Database)」というORMを紹介します。 ORMとEloquent ORMとは? ORMは「Object Relational Mapping (オブジェクト関係マッピング)」と呼ばれている、オプジェクト指向で書かれているプログラムから、オブジェクト指向ではないデータベースなどのデータをオプジェクト指向のように扱う手法のことを言います。 かみ砕いて説明すると、データベースをオブジェクト指向っぽく扱うためのクラス構造ということです。 例えば、 SELECT 'id', 'name', 'age' FROM users WHER
はじめに はじめまして、オープンロジのエンジニアで @ttaka と申します。 この記事は OPENLOGI Advent Calendar 2017 の8日目になります。 さて、弊社サービスのサーバーサイド開発には、 PHP のフレームワークである Laravel が使われています。今回はその Laravel の ORM である Eloquent を使う際に、ちょっとハマりそうなポイントをいくつかご紹介したいと思います。 ハマりポイント 論理削除トレイトはテーブルの結合先では動作しない 論理削除自体には賛否ありますが、 Eloquent には標準でその機能が提供されています。 例えば、次のような論理削除を使用している User と Profile クラスがあるとします。 class User extends Model { use SoftDeletes; public function
Laravelで複数レコードを一気にテーブルに追加したい. foreach() & save() はなんとなくイケてない気がする. こんなモチベーションでLaravelにおけるDB格納方法を調査した. 新規レコード追加方法は主に次の3つらしい. save() create() insert() 今回はこれらの違いについて調査. 参考 Laravel 5.3 Eloquent:利用の開始 | readouble.com Laravel 5.3 データベース:クエリビルダ | readouble.com 動作環境 Laravel 5.3 PHP 5.6.x MySQL 5.7.15 CentOs 6.8 Apache 2.4 前提条件 次の点を前提とする. vagrant + virtualBox で構築したローカル開発環境で動作させる cofig/database.php 設定済 .env
LaravelのQueryBuilderにもjoinがあります。 業務などで主に使うものは、inner joinとleft joinではないでしょうか。 inner joinの場合 join関数を使うことで可能です。 結合条件を、第2、第3、第4引数で表現します。 例) blogsテーブルにarticlesテーブルをinner joinする場合 $students = DB::table('blogs') ->join('articles', 'blogs.id', '=', 'articles.blog_id') ->select('blogs.*', 'articles.title') ->get(); ・left join(left outer join)の場合 leftJoin関数を使うことで可能です。 $students = DB::table('blogs') ->leftJo
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になります)
背景 Laravelアプリケーションでレコードをupdateするやり方が色々あり、戻り値の違いについて調べた。 結論 表にまとめた。 書き方 戻り値の型 更新対象がない時の戻り値 \DB::table(‘samples’)->where($where)->update($values) int(件数) 0 \App\Sample::where($where)->update($values) int(件数) 0 \App\Sample::where($where)->first()->update($values) bool(成否) 実行不可 \App\Sample::find($id)->update($values) bool(成否) 実行不可 詳細 それぞれのupdate呼び出し結果をvar_dumpしてみる。 <?php // 初期データ \DB::table('samples')
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) {
Laravel はWeb職人好みの使いやすいフレームワークです。 Laravelが初期装備している Eloquent ORM はコレクション機能と親和性が高く、組み合わせて利用するメリットは大きいのですが、苦手なものも当然もあります。 そのひとつが「 主キーの変更が苦手 」という点です。 Eloquentで開発した結果、 主キーは変更せず規約に従うのがベター という結論に至りましたので、主キー周りについて私が理解したことをまとめます。 環境は以下の通りです。 PHP 7 Laravel 5.3 Eloquent ORMの主キー規約 Eloquent ORMでは次のように主キーに対する規約を設けております。 符号なしINT (unsigned int) フィールド名はid オートインクリメント この規約に従う場合に限り、とてもシンプルにコードを書くことができます。 スキーマビルダー 規約に従
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く