app/Http/Controllers/Admin/CategoryController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $rules = array(
            'category_name' => 'required',
        );
        $data = [
            'category_name' => trim($request->get('category_name')),
            ];
        $validator = Validator::make($data,$rules);
    if($validator->fails())
       {
        return  response()->json(['errors'=>$validator->errors()]);
       }else
       {
        $user_id = Auth::user()->id;
            if(isset($request->edit_id) && ($request->edit_id !="") )
            {
            $data = Category::findOrFail($request->edit_id);
            $data->category_name = $request->category_name;
            $data->status     = $request->status;
            $data->save(); 
            $success = 'Category has been updated.';
            return response()->json($success);
            }else{
            $data = New Category;
            $data->category_name = $request->category_name;
            $data->user_id     = $user_id;
            $data->status        = 'Active';
            $data->save();
            $success = 'Category has been created.';
            return response()->json($success);
           }
        }
    }
public function store ( Request $ request ) { $ rules = array ( 'category_name' => 'required' , ) ; $ data = [ 'category_name' => trim ( $ request -> get ( 'category_name' ) ) , ] ; $ validator = Validator :: make ( $ data , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ 'errors' => $ validator -> errors ( ) ] ) ; } else { $ user_id = Auth :: user ( ) -> id ; if ( isset ( $ request -> edit_id ) && ( $ request -> edit_id != " " ) ) { $ data = Category :: findOrFail ( $ request -> edit_id ) ; $ data -> category_name = $ request -> category_name ; $ data -> status = $ request -> status ; $ data -> save ( ) ; $ success = 'Category has been updated.' ; return response ( ) -> json ( $ success ) ; } else { $ data = New Category ; $ data -> category_name = $ request -> category_name ; $ data -> user_id = $ user_id ; $ data -> status = 'Active' ; $ data -> save ( ) ; $ success = 'Category has been created.' ; return response ( ) -> json ( $ success ) ; } } }
Store a newly created resource in storage.
Store a newly created resource in storage .