0
0

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 1 year has passed since last update.

Laraveのモデルの更新・更新【個人的なお勉強アウトプット】

Last updated at Posted at 2022-04-08

参考図書

モデルの更新

あらかじめどのモデルを更新するか指定し、モデルの内容を書き換えて保存する。
新規作成はnewでインスタンスを作成したのに対し、更新ではモデルのfindメソッドで更新するモデルを取得する。

app/Http/Controllers/PersonController.php
public function edit(Request $request){
$person = Person::find($request->id);
return view('person.edit', ['form' => $person]);
}

public function update(Request $request){
$this->validate($request, Person::$rules);
$person = Person::find($request->id);
$form = $request->all();
unset($form['_token']);
$person->fill($form)->save();
return redirect('/person');
}

モデルの削除

app/Http/Controllers/PersonController.php
public function delete(Request $request){
$person = Person::find($request->id);
return view('person.del', ['form' => $person]);
}

public function remove(Request $request){
Person::find($request->id)->delete();
return redirect('/person');
}

削除したいモデルの対象をfindで検索してdeleteメソッドをよびだすだけ。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?