dimanche 25 septembre 2016

new created model with related table output and then to json

How do get the related user table of a newly created Contact model and then in the response header content-length out put it toJson().

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()]);
    }               



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire