2017-10-01 7 views
0

私はLaravelとJsonをかなり新しくしています。Laravel - DBに保存する前にJSONヘッダーを削除します

JSONデータをMYSQLテーブルに直接保存することを検討しています。すべてが正常に動作しているが、これは私がどの格納されている取得しています出力されます:私は、これは出力DBに保存され、他のすべてが無視になりたい

HTTP/1.0 200 OK 
Cache-Control: no-cache, private 
Content-Type: application/json 
Date:   Sun, 01 Oct 2017 04:10:34 GMT 

{"_token":"53jnwIYCnLu1jeHdSVcL75Mgw2OD6RmZAh7Ojdyy","name":"adfadfdafd"} 

{"_token":"53jnwIYCnLu1jeHdSVcL75Mgw2OD6RmZAh7Ojdyy","name":"adfadfdafd"} 

ここれます私のコントローラ:

public function addleads(Request $request) 
    { 
     $lead = new Lead; 
     $lead->lead_data = response()->json($request); 
     $lead->save(); 
    } 

ビュー

  <!-- load jQuery --> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 

      <!-- provide the csrf token --> 
      <meta name="csrf-token" content="{{ csrf_token() }}" /> 
      <input class="name"></input> 
      <button class="postbutton">Post via ajax!</button> 
      <script> 
       $(document).ready(function(){ 
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content'); 
        $(".postbutton").click(function(){ 
         $.ajax({ 
          /* the route pointing to the post function */ 
          url: "{{ route('addleads') }}", 
          type: 'POST', 
          /* send the csrf-token and the input to the controller */ 
          data: {_token: CSRF_TOKEN, name:$(".name").val()}, 
          dataType: 'JSON', 
          /* remind that 'data' is the response of the AjaxController */ 
          success: function (data) { 
           console.log(JSON.parse(data)); 
          } 
         }); 
        }); 
       });  
      </script> 

答えて

0

あなたのコードに基づいて、あなたが実際に応答JSONを取得したいされていない場合は、この

public function addleads(Request $request) 
{ 
    $lead = new Lead; 
    $lead->lead_data = response()->json($request)->getContent(); 
    $lead->save(); 
} 

を使用することができますが、要求JSONは、あなたはあなただけ抽出する必要が$request->json()またはjson_encode($request->all())

+0

このエラーを取得: 「D:\ xamppの\ htdocsに\ leadcapture \ベンダ\ laravel \枠組み\ SRC \を照らし\ Suppor を提出、このような要求とセーブから必要なデータを、 T \ Str.php」 ライン :メッセージ : は "クラスのSymfony \コンポーネントのオブジェクト\ HttpFoundation \ ParameterBagは、')(代わりに '$要求 - > JSONの、私は思います –

+0

" という文字列に変換することができませんでした'$ request-> all()'を使うと、すべてのパルメータを取得できます。 –

+0

@RajaAbbas古いコードに基づいてコードを更新しました。それはsymfonyの機能です –

0

呼び出すことができます

public function addleads(Request $request) { 
    $lead = new Lead; 
    $lead->lead_data = json_encode($request->only('_token', 'name')); 
    $lead->save(); 
} 
関連する問題