2017-09-07 20 views
0
public function orgPurokEdit($orgId, $purok, $newPurokName) 
    { 
     $updated = Data::where('org_id', $orgId) 
     ->where('purok', $purok) 
     ->update(['purok' => $newPurokName]); 

     if($updated){ 
      $this->success = true; 
     }else 
     { 
      $this->errors = "Not updated"; 
     } 

     return response()->json([ 
      'success' => $this->success, 
      'errors' => $this->errors 
     ]); 
    } 

これはコードですが、データは更新されませんでした。これに何か問題はありますか?助けを前にありがとう。Laravelで更新できませんでした

+2

それは 'true'を返すのか、' updated'ないですか? – Jackowski

+0

列を塗りつぶし可能な配列に追加しましたか? –

+0

これはresponse.success = falseを返す – Jen143

答えて

1

Laravelの更新はウル条件に一致する行がない場合は更新機能があっても0を返します、に言い換えれば、を変更されていると、クエリが正常かどうかが実行されていない場合は、行の数を返します。それ例外やエラーは発生しませんでした。だから、必ずあなたの条件に一致する行があるようにする

は、あなたがすることによって得たどのくらいの行点検し、私たちが知ってみましょう:

$count = Data::where('org_id', $orgId)->where('purok', $purok)->count(); 

を、それが0より大きいだならば、あなたの更新クエリは確かに動作します。

やコードを配置するためのより良い方法は次のようになります。

public function orgPurokEdit($orgId, $purok, $newPurokName) 
    { 
     $query = Data::where('org_id', $orgId)->where('purok', $purok); 

     if($query->count() == 0){ 
      //There are no entries to update 
     }else 
     { 
      $updated = $query->update(['purok' => $newPurokName]); 
      // $updated."rows has been updated"; 
     } 
    } 

はそれが役に立てば幸い:)

関連する問題