質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

1000閲覧

laravel heroku でApplication errorがでた

ORyohei

総合スコア18

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2020/08/05 14:37

編集2020/08/05 14:38

laravelでherokuにデプロイしています。リクエストを送ると以下のエラーが出ます。調べると30秒以上かかる処理があると以下のエラーが出るようです。実際、ものすごく重いです。laravelでherokuにデプロイした時に、処理を早くする方法で何かいいアドバイスありますでしょうか。

Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command heroku logs --tail 2020-08-05T13:24:04.712483+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/orders/101/done" host=haiki.herokuapp.com request_id=630a64a4-98d6-4fdb-a1e4-d1cfec116afd fwd="126.204.169.165" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=http
public function done(Request $request,$id) { $product = Product::find($id); $user = $product->user; $buyer = Auth::guard('buyers')->user(); Log::debug(print_r( $buyer, true)); //ここからの処理が重い $product->orderBuyer()->detach( Auth::guard('buyers')->user()->id); //二重登録防止のため、一度消去 $product->orderBuyer()->attach( Auth::guard('buyers')->user()->id); //$product->orders()でproductモデルからordersテーブル経由で紐付いているバイヤーモデルのコレクションが返 $shop =$user; $buyer_name =$buyer; $to = [ 'test_01@mail.com', 'test_02@mail.com' ]; Mail::to($to)->send(new PurchaseNotification($shop, $product,$buyer_name)); return redirect()->route('products.index')->with('flash_message', '購入が完了されました');; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

おそらく、lazy load によって N + 1 問題 が発生しているものと思われます

Eloquent: Relationships - Laravel - The PHP Framework For Web Artisans

When accessing Eloquent relationships as properties,
the relationship data is "lazy loaded".
This means the relationship data is not actually loaded
until you first access the property.
However, Eloquent can "eager load" relationships at the time you query the parent model. > Eager loading alleviates the N + 1 query problem.

Heroku のデータベースのクエリーログは次のコマンドで確認できるようです:

console

1heroku logs -p postgres -t

ただし、データベースのロギングは production-tier のみで、
hobby-tier の場合はログは確認できないようです

参考: Understanding Heroku Postgres Log Statements and Common Errors | Heroku Dev Center

N + 1 問題 が発生している場合は、eager load を行うことで問題を解消できます

参考:
N+1問題 - Qiita
ORM の eager loading と lazy loadingについて|withnicのWebエンジニアな日々

投稿2020/08/05 15:09

編集2020/08/05 15:11
y_shinoda

総合スコア3272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ORyohei

2020/08/06 03:21

ご回答ありがとうございます!これらの知識はまるでなかったので大変助かりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問