2016-09-20 8 views
1

Javascriptを初めて使用しています。私は私のフォームでAJAXの更新をしたいです。すべてうまく動作します。更新が完了すると、データベース内の値が更新されます。問題が発生するのは、ビューで更新しようとするときです。私のコントローラはこちらですlaravelのコントローラーからのJSON応答を返す

public function updatePost(Request $request) 
{ 

    $post = $request->all(); 
    $val = \Validator::make($request->all(), [ 

      'client_id' => 'required', 
      'client_name' => 'required', 
      'client_business' => 'required', 
      'client_ref' => 'required', 
      'gmail_mail' => 'required', 
      'gmail_pass' => 'required', 
      'client_dob' => 'required', 
      'client_addr' => 'required', 
      'client_no1' => 'required', 
      'client_ref' => 'required', 
      'domain_name' => 'required', 
      'domain_p_date' => 'required', 
      'domain_reg' => 'required', 
      'domain_ex_date' => 'required', 
      'domain_acc_email' => 'required', 
      'domain_acc_pass' => 'required', 
      'domain_id' => 'required', 

     ]); 

      if ($val->fails()) { 

      return redirect()->back()->withErrors($val->errors()); 
     } 


      else { 

       $data = array(

       'client_id' => $post['client_id'], 
       'client_name' => $post['client_name'], 
       'client_business' => $post['client_business'], 
       'client_ref' => $post['client_ref'], 
       'gmail_mail' => $post['gmail_mail'], 
       'gmail_pass'=> $post['gmail_pass'], 
       'client_dob'=> $post['client_dob'], 
       'client_addr'=> $post['client_addr'], 
       'client_no1'=> $post['client_no1'], 

       'domain_name' => $post['domain_name'], 
       'domain_p_date' => $post['domain_p_date'], 

       'domain_reg' => $post['domain_reg'], 
       'domain_ex_date' => $post['domain_ex_date'], 
       'domain_acc_email' => $post['domain_acc_email'], 
       'domain_acc_pass' => $post['domain_acc_pass'], 
       'domain_id' => $post['domain_id'], 

       ); 

      //var_dump($data); 

      $update_data = domain_details::where('domain_id', $post['domain_id']) 
      ->update($data); 

       if ($update_data) { 

        $new_data = domain_details::where('domain_id',$post['domain_id'])->get(); 

        return response()->json(['client_id'=> 'johncena'],200); 
       } 
       else 
       { 

        return redirect()->back()->withErrors($val->errors()); 
       } 



     } 

これは私がフォームから受け取ったデータです。データベースの更新はうまく動作します。問題は

if ($update_data) { 

$new_data = domain_details::where('domain_id',$post['domain_id'])->get(); 

return response()->json(['client_id'=> $new_data->client_id],200); 

JSON([ 'CLIENT_ID' => $ new_data-> CLIENT_ID]、200)が発生する場所です。私はちょうどjのようにうまく動作する文字列を渡す場合son(['client_id' => 'John Doe']、200);

もう1つの問題は、コンソールが表示されていることです。何も変更せずにフォームを更新しようとすると、500の内部サーバーエラーがスローされます。ここでコントローラからの応答を処理する私のJavascriptコードです。いつか文字列がJohn Doeに変更され、ほとんどの場合、私は500内部サーバーエラーを取得します。

$('#update-modal').on('click',function(){ 

$.ajax({ 

    type : "get", 
    url : updateURL, 
    data : { client_id : $('#client_id').val(), 
      client_name : $('#client_name').val(), 
      client_business : $('#client_business').val(), 
      client_ref : $('#client_ref').val(), 
      gmail_mail : $('#gmail_mail').val(), 
      gmail_pass : $('#gmail_pass').val(), 
      client_dob : $('#client_dob').val(), 
      client_addr : $('#client_addr').val(), 
      client_no1 : $('#client_no1').val(), 

      domain_name : $('#domain_name').val(), 
      domain_p_date : $('#domain_p_date').val(), 
      domain_reg : $('#domain_reg').val(), 
      domain_ex_date : $('#domain_ex_date').val(), 
      domain_acc_email : $('#domain_acc_email').val(), 
      domain_acc_pass : $('#domain_acc_pass').val(), 
      domain_id : domain_id, 
      _token : token 
     } 

}) 
.done(function(msg){ 
    $(client_id_element).text(msg['client_id']); 



}); 
}); 

私は何かが不足していることを知っています。私はそれが何であるか把握できません。事前に感謝

答えて

2

まあ、私はEloquent get()は、特定のオブジェクトではなく、コレクションを返すと思います。一意のdomain_idのオブジェクトを1つだけ返すように見えます。代わりに

$new_data = domain_details::where('domain_id',$post['domain_id'])->first(); 

を試してみてください。

+0

これは魅力的な働きをしていました。私はJSONデータで間違いを犯していると思いました。そして構文 –

+0

'get()'を使うと、クエリ結果にアクセスする適切な方法は、コレクション全体をループし、各オブジェクトを操作するか、 '$ new_data [0] - > client_id'のようなインデックス表記を使うことですそれは良くありません。あなたのDBから取得しようとしているオブジェクトが 'id'のような一意のプロパティを持っていることを確信しているならば、' first() 'を使って一つのアイテムだけを得ることができます。私はそれが働いて満足している:) – Khallister

関連する問題