Priya
Priya

Reputation: 129

check if value exists update else insert laravel

I have a function where what i want to do first check order_id exists it update else insert. Please suggest me

public function possstContract(){


        $postContracts = Contract::findOrNew(Input::get('order_id'));


    }

Upvotes: 1

Views: 884

Answers (1)

Jeff
Jeff

Reputation: 25221

public function postContract(){

    $postContracts = Contract::updateOrCreate([
            'order_id'=>Input::get('order_id')
        ],[
            'content'=>Input::get("content"),
            'signature'=>Input::get("signature"),
            'created_by'=>Input::get("created_by"),
            'updated_by'=>Input::get("updated_by"),
        ]);

     return Response::json([
            "success" => true,
            "message" => "Contract Added Successfully"
        ], 200);

updateOrCreate() looks for a model with the first array of values, and then either creates it or just updates it with the second array of values.

Upvotes: 2

Related Questions