Laravel 更新クエリ 質問する

Laravel 更新クエリ 質問する

ID ではなくメールに基づいてユーザーを更新しようとしていますが、生のクエリを使用せずにこれを行う方法はありますか。

現在エラー

{"error":{"type":"ErrorException","message":"空の値からデフォルトのオブジェクトを作成しています","file":"C:\wamp\www\celeb-jim\app\controllers\SignupController.php","line":189}}

私のコード

    public function changeAccountStatus ($plan, $userEmail ){
        $UpdateDetails = User::where('email', '=',  $userEmail)->first();
        $UpdateDetails->member_type = $plan;
        $UpdateDetails->save();
    }

ベストアンサー1

あなたはLaravel クエリビルダー、 しかしこれは最善の方法ではない

チェックウェイダーの回答は以下Eloquent の方法では、実際に電子メール アドレスに一致するユーザーが存在するかどうかを確認し、存在しない場合はエラーを処理できるため、より優れています。

DB::table('users')
        ->where('email', $userEmail)  // find your user by their email
        ->limit(1)  // optional - to ensure only one record is updated.
        ->update(array('member_type' => $plan));  // update the record in the DB. 

更新するフィールドが複数ある場合は、配列の最後に値を追加するだけです。

おすすめ記事