Farshad
Farshad

Reputation: 2000

how to manage laravel big update methods

i have an update method like below which is so big and i want to manage it some how that in take less place in controller and make controller much cleaner now i want to know if there is any way to make it as service or some thing this is my update method for example :

      public function update(Request $request, Something $something)
    {

        $something->somefield = $request->get('field1');
        $something->somefield = $request->get('field1');
        $something->somefield = $request->get('field1');
        $something->somefield = $request->get('field1');
        $something->save();
    return response()->json($something, 200);

  //consider i may have like 20 fields here

Upvotes: 1

Views: 65

Answers (3)

Nikhil Kamath
Nikhil Kamath

Reputation: 11

Use below code in case fields not present in db passed.

$something->update($request->only($field1, $field2));

Upvotes: 1

zahid hasan emon
zahid hasan emon

Reputation: 6233

For me the appropriate way to do this is to name the input fields of the form and fields of the table same. Then you can just use $something->update($request->all());

Upvotes: 1

Karan
Karan

Reputation: 1156

Use update() method to update all fields

public function update(Request $request, Something $something)
{

        $something->update($request->all());
        return response()->json($something, 200);

}

Upvotes: 1

Related Questions