2016-09-26 12 views
-2

新しく作成されたContactモデルの関連ユーザーテーブルを取得し、次にレスポンスヘッダーのcontent-length outをJson()に渡すにはどうすればよいですか。新しく作成された関連テーブル出力とjsonへ

public function store(Request $request) { 

    try { 

     $contact = new Contact(); 
     $contact->email_address = Helper::strip_tags($request->get('email_address')); 
     $contact->firstname = ucfirst($request->get('firstname')); 
     $contact->lastname = ucfirst($request->get('lastname')); 
     $contact->company = ucfirst($request->get('company')); 
     $contact->phone = $request->get('phone'); 
     $contact->mobile = $request->get('mobile'); 
     $contact->description = Helper::strip_tags($request->get('description'));     

     if($contact->save()) {     

      // here is the part I'm having trouble with 

      $contact = $contact->with('user')->get();  

      return response()->json($contact, 200, ['Content-Length' => strlen($contact->toJson())]); 

     } else { 

      return response()->json(array('error' => true, 'messages' => $contact->errors), 400);  
     } 

    } catch(Exception $e) { 
     return response()->json(array('error' => true, 'type' => 'exception', 'message' => $e->getMessage()), 500, ['Content-Length' => $e->getMessage()]); 
    }    

答えて

0

(あなたがそれを作成したときに)あなたはすでにモデルがロードされてきたように、それはイーガーローディング関係のためであるとして、あなたはwithを使用することはありません。

私はあなたがUser関係はあなたが$contact->load('user');

この情報がお役に立てば幸いを行うだろうので、あなたが「怠惰な積極的なロード」を使用し、出力に含まれ得るために、正しく質問を理解している場合!

関連する問題