だから基本的に私はこれを可能な限り最善の方法で行う方法について質問があります。そして、そもそもそれを正しく行っているかどうかさえわかりません。
ユーザーがいくつかの設定を更新できる設定ページが表示されました。フォームが送信されると、ご覧のとおり、以下の方法でリクエストが処理されます。
私はProfile
モデルだけでなくモデルも持っており、User
それらの関係も正しく設定されています。
しかし、ご覧のとおり、このメソッドではすべてがコントローラーで実行されます。Profile
モデルは一切使用していません。しかし、私はすべきではありませんか?
Profile
コントローラーでの作業を減らすために、モデルでどのような方法を作成して使用できますか?
これが不適切な質問である場合は申し訳ありません。
前もって感謝します。
/**
* @return mixed
*
* Process general settings
*/
public function postEditGeneralSettings() {
// Validate
$val = Validator::make([
'show_age' => Input::get('show_age'),
'show_gender' => Input::get('show_gender')
], [
'show_age' => 'sometimes|boolean',
'show_gender' => 'sometimes|boolean'
]);
if ($val -> fails()) {
return $this -> backWithErrors($val);
}
// Update
Auth::user() -> profile() -> update([
'show_age' => Input::get('show_age'),
'show_gender' => Input::get('show_gender')
]);
return $this -> backWithSuccess('Innstillingene ble lagret!');
}
リポジトリクラスを使用して、コントローラからデータを保存する責任を押し出すことができます。
class UserRepository
{
//rules for validation, as an alternative you can put them in your User Model
public static $rules = [
'title' => 'required|unique|max:255',
'body' => 'required',
];
public function updateUserProfile($user, $data)
{
$user->profile()->update([
'show_age' => $data('show_age'),
'show_gender' => $data('show_gender')
]);
}
次に、コントローラーからリポジトリーを使用します。
public function postEditGeneralSettings(UserRepository $repo)
{
//an alternative to your validation, using the ValidatesRequests trait of the controller
//this will give the same results of your validation, but is more concise
$this->validate( CUserRepository::$rules, Input::all() );
// Update using the repository
$repo->update( Auth::user(), Input::all() );
return $this->backWithSuccess('Innstillingene ble lagret!');
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加