79
64

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) fillを使ってモデルの複数カラムを更新する

Posted at

公式マニュアルではモデルを更新するとき、プロパティを指定して値を入れる方法が紹介されている。

$flight = App\Flight::find(1);

$flight->name = 'New Flight Name';

$flight->save();

しかし、これは更新したいプロパティが複数あるときは、つらつらと並べないといけない。

そんなときはfillを使うと便利。

$flight->fill(['name' => 'New Flight Name'])->save();

Arrayで指定できるので以下のようにすることもできる。

public function update(Request $request)
{
    $flight = App\Flight::find($request->id);
    $flight->fill($request->all())->save();
}

こうすればいちいちプロパティを指定せずに済むので便利ですね。

79
64
2

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
79
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?