如果没有该ID的记录,如何使用ID从数据库获取记录,使该ID递增并在laravel中获取记录

时间:2019-07-10 06:24:36

标签: laravel

如何使用ID从数据库获取记录,如果没有该ID的记录,则在Laravel中递增该ID并使用递增的ID获取记录 即:我正在传递ID 50,如果没有ID(50)的记录,则尝试51.如果未找到51的结果,则尝试52 ...

我尝试将id增大为50 + 1,但不适用于每种情况。 如果没有可用的ID,则如何格式化此代码:

我告诉你清楚的询问 像这样的表结构

id    Personid

50   1
51   2
52   null
53   null
54   null
55   null
56   null
57   5
58   6

如果我通过ID 52,则它将搜索ID 52 52没有价值 然后它应该搜索下一个id 53 thats null。 所以应该用id 57

Laravel代码:

public function getApplicantPopupDetails($id, $start = null) {
    $out = [];
    $array = [];
    $out = $this->initOut();
    $PersonId = $id;

    $applicant = DB::table('applicants')
                    ->where('applicants.PersonId', '=', $PersonId)
                    ->get(['Name','Address','Surname','country','Address','ContactNo','Email','created_at','imgUrl']);
    if($applicant->isEmpty()){

        if($start == 0){
            $ids = $PersonId - 1;
        } else {
            $ids = $PersonId + 1;
        }
        $array['main'] = $applicant;
        $this->getApplicantPopupDetails($ids,0);
    }

    $out['results'] = $array;
    return $out;
}

我希望上面的结果不会有任何问题。

4 个答案:

答案 0 :(得分:0)

从您的代码中,我了解到您想与PersonId >= $id

只需使用以下代码即可完成

public function getApplicantPopupDetails($id, $start = null) {
    $out = [];
    $array = [];
    $out = $this->initOut();
    $PersonId = $id;

     DB::table('applicants')
            ->select('Name','Address','Surname','country','Address','ContactNo','Email','created_at','imgUrl');
            ->where('applicants.PersonId', '>=', $PersonId)
    if($applicant->isEmpty()){

        if($start == 0){
            $ids = $PersonId - 1;
        } else {
            $ids = $PersonId + 1;
        }
        $array['main'] = $applicant;
    }

    $out['results'] = $array;
    return $out;
}

答案 1 :(得分:0)

请尝试这种方式,

public function getApplicantPopupDetails($id, $start = null) {
    $out = [];
    $array = [];
    $out = $this->initOut();
    $PersonId = $id;

    $applicant = DB::table('applicants')
                    ->select('Name','Address','Surname','country','Address','ContactNo','Email','created_at','imgUrl')
                  ->where('applicants.PersonId', '=>', $PersonId)
                   ->first();
   if($applicant->isEmpty()){

       if($start == 0){
           $ids = $PersonId - 1;
       } else {
          $ids = $PersonId + 1;
       }
      $array['main'] = $applicant;
     $this->getApplicantPopupDetails($ids,0);
    }

    $out['results'] = $array;
    return $out;
}

答案 2 :(得分:0)

public function getApplicantPopupDetails($id, $start = null) {
    $out = [];
    $array = [];
    $out = $this->initOut();
    $PersonId = $id;

    $applicant = DB::table('applicants')
                    ->select('Name','Address','Surname','country','Address','ContactNo','Email','created_at','imgUrl')
                  ->where('applicants.PersonId', '=>', $PersonId)
                   ->first();
   if($applicant->isEmpty()){

    	$newID = $id+1; //P1
    	$newquery = $this->getApplicantPopupDetails($newID,0); //recursive function
      $array['main'] = $applicant;
    }

    $out['results'] = $array;
    return $out;
}

P1:只需在给定的$ id上加1并递归调用present函数,希望对您有帮助:)

答案 3 :(得分:0)

这正在工作(感谢支持人员)

public function getApplicantPopupDetails($id, $start = null) {
    $out = [];
    $array = [];
    $out = $this->initOut();
    $PersonId = $id;

    $applicant = DB::table('applicants')
                    ->where('applicants.PersonId', '=', $PersonId)
                    ->get(['Name','Address','Surname','country','Address','ContactNo','Email','created_at','imgUrl']);
    if($applicant->isEmpty()){

            $applicant = DB::table('applicants')
                    ->where('applicants.PersonId', '>=', $PersonId)
                    ->orderBy('PersonId' ,'ASC')
                    ->first(['PersonId','Name','Address','Surname','country','Address','ContactNo','Email','created_at','imgUrl']);
    }

    $out['results'] = $array;
    return $out;
}