Farhanah
1 week ago
42
0
Lumen

How to call the data from lumen in laravel controller to check duplicate data

Posted 1 week ago by Farhanah

I have to make ajax call to check duplicate data.I am very new to this and desperately need help. I think I need to write something similar like this in the laravel controller but how to do that?

if($request->get('email'))
     {
      $email = $request->get('email');
      $data = DB::table("tbl_login")
       ->where('email', $email)
       ->count();
      if($data > 0)
      {
       echo 'not_unique';
      }
      else
      {
       echo 'unique';
      }

The code in the controller in lumen api.

public function check_campaign_title(Request $request) {
      try {
       
        $campaign= Campaign::where([
            ['title', $campaign_name],
            ['bank_id', $campaign_provider],
        ])
        ->count();
    
        $response = array(
            'status' => 'success',
            'message' => 'Successfully fetched name',
            'campaign' => $campaign,
        );
        return response()->json($response);

    } catch(\Exception $e) {
        // When query fails. 
        $response = array(
            'status' => 'failed',
            'message' => 'Failed to fetch name '. $e->getMessage(),
        );

        return response()->json($response);
    }
    }

the code in laravel controller


public function check_campaign_name(Request $request){
        $admin = new Admin_id;
        $admin_user_id = $admin->admin_id();

        $domain = Config::get('globalvariables.gcr_api'); 
        $url = $domain . "/api/v1/campaign/check_title";
        $campaign_title =  $request->input('campaign_title');
        $campaign_provider =  $request->input('campaign_provider');
    
        $form_param = [
            'title' => $campaign_title,
            'bank_id' => $campaign_provider,
        ];
        if ($campaign_title == 'campaign_title' && $campaign_provider =='campaign_provider'){
            $form_param['unique'] = $unique;
        }else{
            $form_param['not_unique'] = $not_unique;

        }

        $HttpReq = new HttpRequest;
        $data = $HttpReq->post($url , $form_param); 
        if ($data['status'] == 'success') {
            return redirect()->back()->with('success_message', $data['message'] )->withInput();

        } else {
            return redirect()->back()->with('fail_message', $data['message'])->withInput();
        }    
    }

My Ajax function


        $(document).ready(function(){
        $('#campaign_title').blur(function(){
        var skudiv2 = '';
        var title = $('#campaign_title').val();
        var bank = $('#campaign_provider').val();
        $.ajax({
        url:"{{ route('campaign.check') }}",
        method:"POST",
        data:{title:title,bank_id:bank_id},
        success:function(result)
        {
            if(result == 'unique')
            {
            $('#skudiv2').html('<label class="text-success">Title Available</label>');
            $('#campaign_title').removeClass('has-error');
            $('#btnCreateCampaign').attr('disabled', false);
            }
            else
            {
            $('#skudiv2').html('<label class="text-danger">Title not Available</label>');
            $('#campaign_title').addClass('has-error');
            $('#btnCreateCampaign').attr('disabled', 'disabled');
            }
        }
        })
        
        });

        });

Please sign in or create an account to participate in this conversation.