【问题标题】:How to change hasone relationship to hasmany relation ship in laravel如何在laravel中将hasone关系更改为hasmany关系
【发布时间】:2015-03-11 13:33:25
【问题描述】:

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

 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

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

【问题讨论】:

    标签: php mysql laravel-4 eloquent


    【解决方案1】:

    试试这个:

    在模范学生中:

    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');
      }
    
    }
    

    【讨论】:

    • 不应该是$attendences = Student::find(1)-&gt;post()吗?应该调用函数名,而不是模型。为简单起见,将Student类中的函数名改为public function attendence()并调用Student::find(1)-&gt;attendence()。
    • 对不起,我很困惑,我已经修复了这个功能。 @蒂姆刘易斯
    • 不用担心,看起来更好:) 只是在-&gt;attendence 之后缺少()
    • 请将答案标记为正确。 @lingowiz 用户lingowiz
    猜你喜欢
    • 2016-03-17
    • 2020-01-18
    • 1970-01-01
    • 2020-01-08
    • 2023-01-12
    • 2020-10-06
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多