如何在模型的雄辩关系中搜索术语?

问题描述

有一个名为Profile的具有用户关系的模型。

    public function user(){
        return $this->belongsTo(User::class);
    }

,配置文件表列为: id | user_id |图片电话

问题是如何从个人资料中搜索用户名?

$term = request()->term;
$profiles = Profile::with('user')->where('name','like','%'.$term.'%')->get();

然后,我想向个人资料显示其名称来自搜索字词。

解决方法

使用Rank Sales EUR = RANKX( ALL(Regions[Region]),CALCULATETABLE( {[Sales Total]},TREATAS({"EUR"},'Dashboard Currency'[Currency]),FILTER(Dates,Dates[Year]=MAX([Year])))) 引用链接https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence

Option Compare Database
Option Explicit

Private Sub UpdateXL()

Dim xl As New Excel.Application
Dim wb As Excel.Workbook
Dim wr As Excel.Worksheet
Dim ws As Excel.Worksheet
Dim LastRow As Long
Dim i As Long
Dim lr As Long

Set xl = CreateObject("Excel.Application")
xl.Visible = True

Set wb = xl.Workbooks.Open("C:DestinationPath.xlsm")

Set wr = Worksheets("Sheet1")

Set ws = Worksheets("Sheet2")

 LastRow = ws.Cells(ws.Rows.Count,1).End(xlUp).Row

    'Copies then cuts the data from "SampleFile" Sheet1" and paste the same in sheet2
   
    With wr
        'LastRow = .Cells(.Rows.Count,1).End(xlUp).Row  ' 
        
        lr = wr.Cells(.Rows.Count,1).End(xlUp).Row
        
        If Not lr = 1 Then .Range("A2:B" & lr).Cut ws.Range("A" & LastRow + 1 & ":" & "B" & LastRow + lr - 1) 'Cut
                
    End With
    

    With ws
        For i = 2 To .Cells(.Rows.Count,"B").End(xlUp).Row
         .Cells(i,"B") = Trim(.Cells(i,"B"))
            Select Case .Range("B" & i)
                Case "FXV","FST","FLB","FFH","FFJ"
                    .Range("C" & i) = "Updated"
            End Select
        Next i
    End With
End Sub
,
  $filterUser = function ($q) use ($term) {
                    $q->where('name','like','%' . $term. '%');
                };
  
  $term = request()->term;
  $profiles = Profile::whereHas('user',$filterUser)->with(['user'=> $filterUser])->get();

如果您要在语法上优化代码并希望更频繁地使用它,请在模型中添加此方法

public function scopeWithAndWhereHas($query,$relation,$constraint){
    return $query->whereHas($relation,$constraint)
                 ->with([$relation => $constraint]);
}

和用法:

 $term = request()->term;
 $profiles = Profile::withAndWhereHas('user',function($q) use ($term){
    $q->where('name','%' . $term. '%');
})->get();
,

您需要将其范围设置在子查询上,如下所示:

$profiles = Profile::with(['user' => function ($query) {
    $query->where('name','%'.$term.'%');
}])->get();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...