app/Http/Controllers/Admin/TestimonialController.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(
            'title' => 'required',
            'description' => 'required',
        );
        $data = [
            'title' => trim($request->get('title')),
            'description' => trim($request->get('description')),
            ];
        $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 = Testimonial::findOrFail($request->edit_id);
            $data->title = $request->title;
            $data->description = $request->description;
            $data->title = $request->title;
            $data->user_id = $user_id;
            if($request->hasfile('image'))
             {
              $file = $request->file('image');
              $image  = time().$file->getClientOriginalName();
              Storage::disk('public')->put('testimonial/'.$image,  File::get($file));
              $data->image     = $image;
             }
            $data->save(); 
            $success = 'Testimonial has been updated.';
            return response()->json($success);
            }else{
            $data = New Testimonial;
            $data->title = $request->title;
            $data->description = $request->description;
            $data->title = $request->title;
            $data->user_id = $user_id;
            if($request->hasfile('image'))
             {
              $file = $request->file('image');
              $image  = time().$file->getClientOriginalName();
              Storage::disk('public')->put('testimonial/'.$image,  File::get($file));
              $data->image     = $image;
             }
            $data->save();
            $success = 'Testimonial has been created.';
            return response()->json($success);
           }
        }
    }
public function store ( Request $ request ) { $ rules = array ( 'title' => 'required' , 'description' => 'required' , ) ; $ data = [ 'title' => trim ( $ request -> get ( 'title' ) ) , 'description' => trim ( $ request -> get ( 'description' ) ) , ] ; $ 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 = Testimonial :: findOrFail ( $ request -> edit_id ) ; $ data -> title = $ request -> title ; $ data -> description = $ request -> description ; $ data -> title = $ request -> title ; $ data -> user_id = $ user_id ; if ( $ request -> hasfile ( 'image' ) ) { $ file = $ request -> file ( 'image' ) ; $ image = time ( ) . $ file -> getClientOriginalName ( ) ; Storage :: disk ( 'public' ) -> put ( 'testimonial/' . $ image , File :: get ( $ file ) ) ; $ data -> image = $ image ; } $ data -> save ( ) ; $ success = 'Testimonial has been updated.' ; return response ( ) -> json ( $ success ) ; } else { $ data = New Testimonial ; $ data -> title = $ request -> title ; $ data -> description = $ request -> description ; $ data -> title = $ request -> title ; $ data -> user_id = $ user_id ; if ( $ request -> hasfile ( 'image' ) ) { $ file = $ request -> file ( 'image' ) ; $ image = time ( ) . $ file -> getClientOriginalName ( ) ; Storage :: disk ( 'public' ) -> put ( 'testimonial/' . $ image , File :: get ( $ file ) ) ; $ data -> image = $ image ; } $ data -> save ( ) ; $ success = 'Testimonial has been created.' ; return response ( ) -> json ( $ success ) ; } } }
Store a newly created resource in storage.
Store a newly created resource in storage .