3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PR: フリーランスコネクト株式会社
フリーランスコネクト採用ページ

Eloquentのfresh()とrefresh()の違い

3
Last updated at Posted at 2023-12-11

概要

モデルオブジェクトで使えるfresh()とrefresh()メソッドの違いを記述します。

fresh()

fresh()は、最新のモデルの状態を返却するメソッドです。
メソッドを生やした元のモデルオブジェクトは最新になりません。

例

$user1 = User::firstWhere('name','John');
$user2 = User::firstWhere('name','John');

$user2->update([
    'name' => 'hoge'
]);

$freshUser = $user1->fresh();
dd($user1->name,$freshUser->name);
// John         hoge

refresh()

refresh()は、メソッドを実行したモデルオブジェクトを最新の状態にします。

例

$user1 = User::firstWhere('name','John');
$user2 = User::firstWhere('name','John');

$user2->update([
    'name' => 'hoge'
]);

$user1->refresh();
dd($user1->name);
// hoge

参考

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?