Laravel在哪里查询查询

时间:2019-04-20 13:18:43

标签: php laravel

我有一个空格和一个兴趣表。

我目前能够获取另存为$ spaceList的空间ID的列表,但我希望我的$ query变量能够检索一个与我的$ spaceList变量中的space_id外键与space_id之一匹配的兴趣列表。

public function index() {

        $user_id = auth()->user()->id;
        $user = User::find($user_id);

        $spaceList = Space::where('user_id', $user_id)->pluck('space_id')->toArray();
        $query = Interest::where('space_id', $spaceList);

        $interests = $query->get(); 

        return view('dashboard')->with('space', $user->space)->with('interest', $interests);
}

谢谢,我已经有很长一段时间了。

2 个答案:

答案 0 :(得分:0)

您应该使用whereIn而不是where

$spaceList = Space::where('user_id', $user_id)->pluck('space_id')->toArray();
$query = Interest::whereIn('space_id', $spaceList);

答案 1 :(得分:0)

在Laravel Eloquent中,Querying Relationship Existence正在努力

如果没有在其他地方使用$ spaceList变量,则不需要在这里

CODESEG:
;---------------------------------------------------------

proc printpixel
mov cx,8d ; x coordinates 
mov dx,8d; y coordinates
mov al,4 ;color that is supposed to be printed at the end
;print pixel:
mov bh,0h
mov ah,0ch
int 10h

ret
endp printpixel
;---------------------------------------------------------
PROC LeftButtonClick
mov  ax, 0000h  ; reset mouse
    int  33h        
    cmp  ax,0FFFFh
    jne  NoMouse
    mov  ax, 0001h  ; show mouse
    int  33h
    MouseLP: ; till cx= x of click & dx= y
    mov  ax, 0003h  ; get mouse position and buttonstatus
    int  33h       
    cmp bx, 1      ; check left mouse click
    jne   MouseLP    ; Loop until mouse click
    call findcolor

NoMouse:    
    ret
ENDP LeftButtonClick 
;---------------------------------------------------------
proc findcolor
    xor bh,bh
    mov ah,0dh
    int 10h ;al=pixel number colour
    mov dl,al
    add dl,'0'
    mov ah,02h
    int 21h
ret
endp findcolor

start:
    mov ax, @data
    mov ds, ax
    ; Graphic mode
    mov ax, 13h
    int 10h

    call printpixel
    call LeftButtonClick

    ; Wait for key press
    mov ah,00h 
    int 16h

; Return to text mode
    mov ah, 0
    mov al, 2
    int 10h 

exit:
    mov ax, 4c00h
    int 21h
END start

请注意,要完成这项工作,您需要在兴趣模块中声明空格一对多关系

应该是这样,更多详细信息请参见文档here

$query = Interest::whereHas('spaces', function($query) use ($user_id) {
    $query->where('user_id', '=', $user_id);
});