2016-09-26 6 views
-2

새로 만든 Contact 모델의 관련 사용자 테이블을 얻은 다음 응답 헤더에서 content-length를 가져 와서 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');

희망 할 것이다, 그래서 당신은 "게으른 열망로드"를 사용하는 것이 출력에 포함!