Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 仅在给定ID上执行文本搜索的雄辩方式_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Laravel 仅在给定ID上执行文本搜索的雄辩方式

Laravel 仅在给定ID上执行文本搜索的雄辩方式,laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,我有一个包含两列的表,其中包含一些文本数据。 我要做的是搜索这些列,但只搜索给定的ID。 到目前为止,我得到的是整个数据库的结果,而不考虑提供的ID。这是因为条件设置错误。 我不知道如何正确设置 $qu = \App\AppModels\Quotation::with([ 'products.product', 'paymentTerms', 'deliveryTerms', 'company', 'statu

我有一个包含两列的表,其中包含一些文本数据。 我要做的是搜索这些列,但只搜索给定的ID。 到目前为止,我得到的是整个数据库的结果,而不考虑提供的ID。这是因为条件设置错误。 我不知道如何正确设置

   $qu = \App\AppModels\Quotation::with([
        'products.product',
        'paymentTerms',
        'deliveryTerms',
        'company',
        'status',
        'createdByUser'
    ]);


    if($search_q){
        $qu->where( 'name', '=', $search_q )
        ->orWhere( 'reference', '=', $search_q )
        ->orWhere( 'name', 'LIKE', '%' . $search_q )
        ->orWhere( 'name', 'LIKE', '%' . $search_q . '%' )
        ->orWhere( 'name', 'LIKE', $search_q . '%' )
        ->orWhere( 'reference', 'LIKE', '%' . $search_q )
        ->orWhere( 'reference', 'LIKE', '%' . $search_q . '%' )
        ->orWhere( 'reference', 'LIKE', $search_q . '%' );
    }

    $quotations = $qu->where('created_at', '>=', $records_from)
    ->whereIn('id',array_unique($quotation_ids))
    ->orderBy('created_at','DESC')
    ->get();
您可以使用“参数分组”对
或where
查询进行分组。所以你可以做:

    if($search_q){
    $qu = $qu->where(function ($query) {
        $query->where( 'name', '=', $search_q )
        ->orWhere( 'reference', '=', $search_q )
        ->orWhere( 'name', 'LIKE', '%' . $search_q )
        ->orWhere( 'name', 'LIKE', '%' . $search_q . '%' )
        ->orWhere( 'name', 'LIKE', $search_q . '%' )
        ->orWhere( 'reference', 'LIKE', '%' . $search_q )
        ->orWhere( 'reference', 'LIKE', '%' . $search_q . '%' )
        ->orWhere( 'reference', 'LIKE', $search_q . '%' );
    });
}
您可以使用“参数分组”对
或where
查询进行分组。所以你可以做:

    if($search_q){
    $qu = $qu->where(function ($query) {
        $query->where( 'name', '=', $search_q )
        ->orWhere( 'reference', '=', $search_q )
        ->orWhere( 'name', 'LIKE', '%' . $search_q )
        ->orWhere( 'name', 'LIKE', '%' . $search_q . '%' )
        ->orWhere( 'name', 'LIKE', $search_q . '%' )
        ->orWhere( 'reference', 'LIKE', '%' . $search_q )
        ->orWhere( 'reference', 'LIKE', '%' . $search_q . '%' )
        ->orWhere( 'reference', 'LIKE', $search_q . '%' );
    });
}

经过进一步研究,我找到了答案

  $qu = \App\AppModels\Quotation::with([
        'products.product',
        'paymentTerms',
        'deliveryTerms',
        'company',
        'status',
        'createdByUser'
    ])
    ->whereIn('id',array_unique($quotation_ids));


    if($search_q){

        $qu->where(function ($query) use($search_q) {
            $query->orWhere( 'name', '=', $search_q )
                ->orWhere( 'reference', '=', $search_q )
                ->orWhere( 'name', 'LIKE', '%' . $search_q )
                ->orWhere( 'name', 'LIKE', '%' . $search_q . '%' )
                ->orWhere( 'name', 'LIKE', $search_q . '%' )
                ->orWhere( 'reference', 'LIKE', '%' . $search_q )
                ->orWhere( 'reference', 'LIKE', '%' . $search_q . '%' )
                ->orWhere( 'reference', 'LIKE', $search_q . '%' );
        });
    }

    $quotations = $qu->where('created_at', '>=', $records_from)
    ->orderBy('created_at','DESC')
    ->get();

经过进一步研究,我找到了答案

  $qu = \App\AppModels\Quotation::with([
        'products.product',
        'paymentTerms',
        'deliveryTerms',
        'company',
        'status',
        'createdByUser'
    ])
    ->whereIn('id',array_unique($quotation_ids));


    if($search_q){

        $qu->where(function ($query) use($search_q) {
            $query->orWhere( 'name', '=', $search_q )
                ->orWhere( 'reference', '=', $search_q )
                ->orWhere( 'name', 'LIKE', '%' . $search_q )
                ->orWhere( 'name', 'LIKE', '%' . $search_q . '%' )
                ->orWhere( 'name', 'LIKE', $search_q . '%' )
                ->orWhere( 'reference', 'LIKE', '%' . $search_q )
                ->orWhere( 'reference', 'LIKE', '%' . $search_q . '%' )
                ->orWhere( 'reference', 'LIKE', $search_q . '%' );
        });
    }

    $quotations = $qu->where('created_at', '>=', $records_from)
    ->orderBy('created_at','DESC')
    ->get();

当您没有提供
$search\u q
值时,您是否得到了正确的结果?是的,结果是正确的,它只是在or条件中混合,因此id被忽略。当您没有提供
$search\u q
值时,您能否得到正确的结果?是的,结果是正确的,它只是在or条件中混合了where,所以id得到ignoredYep,这就是实现它的方法。我找到了解决办法。但是谢谢你的回复,这是解决问题的方法。我找到了解决办法。但是谢谢你的回复