FavoriteLoadingAdd to favorites

Save Function (Update and Add in 1 function based on ID)

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class RandomController extends WhateverWhateverController {

    public function saveTermsAndConditions(Request $request){
        if (isset($request->id)) {
            // Update
            try {
                DB::table('table_name')->where('id',$request->id)->update(
                    array(
                        'field'=>$request->field,
                        'content'=>$request->content,
                        'updated_at' => date("Y-m-d H:i:s", strtotime('now')),
                    )
                );
                $termsAndConditions = DB::table('table_name')->where('id', $request->id)->first();
            } catch (\Exception $e) {
                return response()->json($e);
            }
        } else {
            // Add
            try {
                $id = DB::table('table_name')->insertGetId(
                    array(
                        'field' => $request->field, 
                        'content' => $request->content, 
                        'created_at' => date("Y-m-d H:i:s", strtotime('now')),
                        'updated_at' => date("Y-m-d H:i:s", strtotime('now'))
                    )
                );
                $termsAndConditions = DB::table('table_name')->where('id', $id)->first();
            } catch (\Exception $e) {
                return response()->json($e);
            }
        }

        return $termsAndConditions;
    }
}

Pin It on Pinterest

Share This