douji2520 2018-11-24 22:27
浏览 76

Laravel Eloquent模型复杂查询

I am logged in as the first student in the table. I want to retrieve a list of all the students who are within the same department as mine and whom I didn't send a request to yet, using Eloquent model.

enter image description here

//Requestt.php
public function student()
{
    return $this->belongsto('App\Student','id', 'Sender_id','Rec_id');
}

//Student.php
public function requestts()
{
    return $this->hasMany('App\Requestt', 'id');
}

public function department()
{    
    return $this->belongsTo('App\Department');
}

Note: Don't mind the double "t" in requestt.

  • 写回答

2条回答 默认 最新

  • douguo7431 2018-11-25 00:13
    关注

    Possible Solution

    // you may have the logged in student in your hand.
    // for the example, I'll take the first.
    $logged_in_student = Student::first();
    
    $other_students = Student::where('department_id', $logged_in_student->department_id)
        ->whereDoesntHave('requestts', function($query) use($logged_in_student) {
            $query->where('Rec_id', $logged_in_student->id);
        })
        ->get();
    

    Explanation

    The first where clause is to filter students who are in the same department.
    The second whereDoesntHave clause is to filter students who has no requestts from logged in student.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题