php - 如何在 Laravel 中将 hasone 关系更改为 hasmany 关系

标签 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="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');
  }

}

关于php - 如何在 Laravel 中将 hasone 关系更改为 hasmany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28988000/

相关文章:

url - Laravel 4 使用路由将用户名放置到 URL

php - Medoo PHP db 框架 - 选择加入

javascript - 将文件名日期转换为日期时间

php - 函数参数中的尴尬 PHP 类型错误

php - 在 Propel 中使用 toArray() 时是否可以使用 *RECURSION* 删除字段?

mysql - 如何从我在 mysql WorkBench 中设计的 ER 图获取代码?

mysql - 对于 SQL 递归 CTE,为什么它每次 'loops' 时不再次引用 anchor 成员?为什么主播成员只得到一次 'called'?

php - 如何在 PHP 中使用 implode 更新多个数据

php - 背景图像不覆盖背景 Laravel

php - 如何使用 Laravel 允许 WYSIWYG 编辑器并禁用 XSS 攻击?