如何改变与laravel中的hasmany关系船的关系

时间:2015-03-11 13:33:26

标签: php mysql laravel-4 eloquent

我正在根据一些输入搜索学生,但它返回单个数据。我需要单个学生多个日期注意。但它返回单个注意。

 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 =&#34; 2&#34;装置

Attendence表

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

我的查询只返回第一行,我需要所有行。

1 个答案:

答案 0 :(得分:1)

试试这个:

模特学生

class Student extends Eloquent {

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

}

和类似的查询:

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

如果要获得反向查询,可以添加到Attendence Model:

class Attendence extends Eloquent {

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

}