5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelでEloquentのappendsを動的に変える方法

Posted at

jsonとかで使うのでappendsに入れないと取得できなくなるけど
毎回欲しい訳じゃ無い情報なので動的にappendsの中身を変更できないか調べてみた結果

ソースの中身を読んでみるとappend関数自体がある模様
GoogleやQiitaで検索してもすぐにでてこなかったので短いけど投稿しておきます。

$guests = Guest::get();
foreach ($guests as $guest) {
  // 追加したいappendsの内容
  $guest->append('reservation_count');
}
return $guests;

使い道としては下記みたいなクエリが発行される系で必要でした。
http://qiita.com/t_katsura/items/3a5b46bc070778975051

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?