簡體   English   中英

如何在Laravel中將hasone關系更改為hasmany關系船

[英]How to change hasone relationship to hasmany relation ship in laravel

我正在基於一些輸入來搜索學生,但它返回單個數據。我需要單個學生多次約會出席,但它返回單個出席。

 if(!empty($StudentAdmissionData['TripType']))
     {
     $TripType=$StudentAdmissionData['TripType'];
     $studentcheckarray['TripType']=$TripType;
     } else {
      $TripType="";
     }
     if(!empty($StudentAdmissionData['schoolid']))
     {
     $schoolid=$StudentAdmissionData['schoolid'];
    $studentcheckarray['SchoolName']=$schoolid;
     } else {
     $schoolid="";
     }

     if(!empty($StudentAdmissionData['Startdate']))
     {
     $Startdate=$StudentAdmissionData['Startdate'];
     } else {
      $Startdate="";
     }
     if(!empty($StudentAdmissionData['Enddate']))
     {
     $Enddate=$StudentAdmissionData['Enddate'];
     } else {
     $Enddate="";
     }
     if(!empty($StudentAdmissionData['Gender']))
     {
     $Gender=$StudentAdmissionData['Gender'];
     $studentcheckarray['gender']=$Gender;
     } else {
     $Gender="";
     }
     if(!empty($StudentAdmissionData['Age']))
     {
     $Age=$StudentAdmissionData['Age'];
       $studentcheckarray['Age']=$Age;
     } else {
     $Age="";
     }
     if(!empty($StudentAdmissionData['Grade']))
     {
     $Grade=$StudentAdmissionData['Grade'];
    $studentcheckarray['StudentCourse']=$Grade;
     } else {
     $Grade="";
     }
      if(!empty($StudentAdmissionData['AttendenceStatus']))
     {
     $status=$StudentAdmissionData['AttendenceStatus'];

     } else {
     $status="";
     }
     if(!empty($StudentAdmissionData['studentname']))
     {
     $sname=$StudentAdmissionData['studentname'];
     $snamearray=explode("_",$sname);
     $studentfirstname=$snamearray[0];
     $studentlastname=$snamearray[1];
     $studentcheckarray['PersonalFirstName']=$studentfirstname;
    $studentcheckarray['PersonalLastName']=$studentlastname;
     } else {
     $sname="";
     $studentfirstname="";
      $studentlastname="";
     }
      if(!empty($StudentAdmissionData['ParentName']))
     {
     $pname=$StudentAdmissionData['ParentName'];
    $studentcheckarray['GuardianFirstName']=$pname;
    //$studentcheckarray['GuardianLastName']=$parentlastname;
     } else {
     $pname="";
     $parentfirstname="";
     $parentlastname="";
     }
    if(!empty($StudentAdmissionData['AttendenceFrom']))
     {
     $AttendenceFrom=$StudentAdmissionData['AttendenceFrom'];

     } else {
     $AttendenceFrom="";
     }

我的查詢

$StudentAdmissionDetailsbyid = StudentAdmissionModel::where($studentcheckarray)
        ->whereHas('attendance',function($q)
        {
        if(!empty($StudentAdmissionData['Startdate']) && !empty($StudentAdmissionData['Enddate']))
         {
            $q->whereBetween('date', array($Startdate,$Enddate));

            }
            if(!empty($StudentAdmissionData['Startdate']) && empty($StudentAdmissionData['Enddate']))
         {
            $q->where('date', '>', $Startdate);
            }
            if(empty($StudentAdmissionData['Startdate']) && !empty($StudentAdmissionData['Enddate']))
         {
            $q->where('date', '<', $Enddate);
            }
            if(!empty($StudentAdmissionData['AttendenceFrom']))
         {
         if($AttendenceFrom=="all") 
         {
         if($status==0)
         {
         $q->where('toschool', '=', '0')->where('tohome', '=', '0');    
         }
         if($status==1)
         {
         $q->where('toschool', '=', '1')->where('tohome', '=', '1');    
         }
         }
         if($AttendenceFrom=="fromschool") 
         {
          if($status==0)
         {
         $q->where('toschool', '=', '0');   
         } 
           if($status==1)
         {
         $q->where('toschool', '=', '1');   
         } 
         }
         if($AttendenceFrom=="fromhome") 
         {
          if($status==1)
         {
         $q->where('tohome', '=', '1'); 
         } 
          if($status==0)
         {
         $q->where('tohome', '=', '0'); 
         } 
         }

         }

        })
        ->with('attendance') // Optional eager loading, but recommended
        ->with('schollresult')->with('batchresult')->get()->toArray();

在模型中

 public function attendance()
            {
                // Assuming a one-to-one relationship
                return $this->hasOne('StudentAttandenceModel','studentid');
            }

如何將一對一關系更改為一對多關系。

在學生表中,id =“ 2”表示

出勤表

id studentid tohome toschool date
1   2          1        1     2015-03-09
2   2          1         1    2015-03-10
3   2           1        1    2015-03-11

我的查詢是僅返回第一行,我需要所有行。

嘗試這個:

在模范學生中:

class Student extends Eloquent {

    public function attendence()
    {
       return $this->hasMany('Attendence');
    }

}

和這樣的查詢:

$attendences = Student::find(1)->attendence();

如果要使查詢反向,可以將其添加到出勤模型:

class Attendence extends Eloquent {

  public function student()
  {
     return $this->belongsTo('Student');
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM