Laravel雄辩的whereIn与一个查询

时间:2019-04-03 08:21:46

标签: laravel eloquent

在laravel中,我可以使用laravel雄辩的where方法使用许多where规则作为数组,对数据库运行一个查询,以获得所需的结果。

代码:

$where = [];
$where[] = ['user_id', '!=', null];
$where[] = ['updated_at', '>=', date('Y-m-d H:i:s')];
if($request->searchTerm) {
    $where[] = ['title', 'like', '%' . $request->searchTerm . '%'];
}
Model::where($where)->get();

问题部分:

现在,我需要对数组参数使用Laravel雄辩的方法whereIn来通过一个查询获得所需的结果。

我尝试使用 looping 方法,但是对数据库的查询很多。

代码:

$model = new Model;
$whereIn = []; 

$whereIn[] = ['date', '>=', Carbon::now()->subDays(10)];
$whereIn[] = ['user_role', 'candidate'];

if (!empty($scope) && is_array($scope)) {
    $whereIn[] = ['user_id', $scope];
}
if(is_array($employment) && !empty($employment)) {
    $whereIn[] = ['employment', $employment];
}
if(is_array($experience) && !empty($experience)) {
    $whereIn[] = ['experience', $experience];
}

foreach ($whereIn as $v) {
    $model = $model->whereIn($v[0], $v[1]);
}

dump($model->get());

首先我很累$model->whereIn($whereIn)->get(),但这是返回错误。可以使用whereIn进行一次查询而不会循环获得结果吗?

注意:我的$whereIn数组将是动态数组!

3 个答案:

答案 0 :(得分:0)

whereIn是查询构建器函数,因此您不能直接在模型上使用它。相反,您应该创建一个查询构建器实例。我还建议您使用when而不是if语句:

$models = Model::when(!empty($scope) && is_array($scope), function ($query) use ($scope) {
    $query->whereIn('user_id', $scope);
})->when(!empty($employment) && is_array($employment), function ($query) use ($employment) {
    $query->whereIn('employment', $employment);
})->when(!empty($experience) && is_array($experience), function ($query) use ($experience) {
    $query->whereIn('experience', $experience);
})->get(); 

dump($models);

when实际上在第一个参数为true时运行该函数。 conditional clauses下的文档中有更多详细信息。

答案 1 :(得分:0)

由于您的$whereIn变量是一个数组数组,因此它将像:

$model->whereIn($whereIn[0][0], $whereIn[0][1])->get();

如果它只是一个简单的数组,则可以使用:

$model->whereIn($whereIn[0], $whereIn[1])->get();

答案 2 :(得分:0)

善于口才

Code from .html file

 <dx-data-grid name="gridContainerProjectiveSummary" [dataSource]="SummaryData" [showColumnLines]="true"
                          [showRowLines]="true" [showBorders]="true"
                          [allowColumnReordering]="true" [allowColumnResizing]="true" [columnAutoWidth]="true">
                <dxo-grouping [autoExpandAll]="false"></dxo-grouping>
                <dxo-export [enabled]="true" fileName="Post-Eval(RO)"></dxo-export>
                <dxo-selection mode="none"></dxo-selection>
                <dxi-column dataField="columnName" caption="" [groupIndex]="0" cellTemplate="columnNameTemplate" [width]="75" cssClass="columnBold">
                </dxi-column>
                <dxi-column dataField="" caption="" [width]="100"></dxi-column>
                <dxi-column dataField="propertyType" caption="Property type" cellTemplate="propertyTypeTemplate" [width]="125" cssClass="columnBold"></dxi-column>
                <dxi-column caption="own">
                    <dxi-column caption="PT" dataField="own_PT"></dxi-column>
                    <dxi-column caption="NPT" dataField="own_NPT"></dxi-column>
                    <dxi-column caption="Total" dataField="own_Total"></dxi-column>
                </dxi-column>
                <dxi-column data *ngFor="let column of compititor" [caption]="column.channelName">
                        <dxi-column caption="PT" [dataField]="column.comp_PT"></dxi-column>
                        <dxi-column caption="NPT" [dataField]="column.comp_NPT"></dxi-column>
                        <dxi-column caption="Total" [dataField]="column.comp_Total"></dxi-column>
                </dxi-column>
                <dxo-summary>
                    <dxi-group-item column="own_PT" summaryType="sum" displayFormat="{0}" [showInGroupFooter]="false" [alignByColumn]="true">
                        <dxo-value-format type="fixedPoint" precision="2"></dxo-value-format>
                    </dxi-group-item>
                    <dxi-group-item column="own_NPT" summaryType="sum" displayFormat="{0}" [showInGroupFooter]="false" [alignByColumn]="true">
                        <dxo-value-format type="fixedPoint" precision="2"></dxo-value-format>
                    </dxi-group-item>
                    <dxi-group-item column="own_Total" summaryType="sum" displayFormat="{0}" [showInGroupFooter]="false" [alignByColumn]="true">
                        <dxo-value-format type="fixedPoint" precision="2"></dxo-value-format>
                    </dxi-group-item>
                    <dxi-group-item column="comp_PT" summaryType="sum" displayFormat="{0}" [showInGroupFooter]="false" [alignByColumn]="true" valueFormat="fixedPoint"></dxi-group-item>
                    <dxi-group-item column="comp_NPT" summaryType="sum" displayFormat="{0}" [showInGroupFooter]="false" [alignByColumn]="true" valueFormat="fixedPoint"></dxi-group-item>
                    <dxi-group-item column="comp_Total" summaryType="sum" displayFormat="{0}" [showInGroupFooter]="false" [alignByColumn]="true" valueFormat="fixedPoint"></dxi-group-item>
                </dxo-summary>
                <div *dxTemplate="let data of 'columnNameTemplate'" class="center">
                    {{data.data.columnName}}
                </div>
                <div *dxTemplate="let data of 'propertyTypeTemplate'" class="center ">
                    {{data.data.propertyType}}
                </div>
            </dx-data-grid>

或然后使用查询生成器:

Navigation.startTabBasedApp({
      tabs: [
        {
          label: "home",
          screen: "screen1",
          title: "Modules",
          icon: icon,
          navigatorStyle: {
            navBarHidden: false,
            navBarBlur: false,
            navBarTextColor: "#000",
            navBarBackgroundColor: 'green',
          }
        },
        {
          label: "settings",
          screen: "screen2",
          title: "Profile",
          icon: iconsMap.user
        }
      ]