Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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
Php Laravel如何编写具有多个条件但记录在同一用户下的搜索查询_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php Laravel如何编写具有多个条件但记录在同一用户下的搜索查询

Php Laravel如何编写具有多个条件但记录在同一用户下的搜索查询,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有一个库存产品数据库。另外,我有很多用户,他们的产品属于不同的用户。 因此,我需要编写一个具有多个条件的查询,其中搜索需要位于不同的表列中,因此它将是两个->或where()语句。但搜索需要找到属于同一用户的所有产品。但是,如果我有许多->或where()条件,query将开始在其他用户下搜索其他查询条件匹配的产品(“title”或“product\u number”) 那么,我如何更改我的代码,在那里我可以得到所有产品的搜索关键字下的产品在同一个用户 这是我的密码: public funct

我有一个库存产品数据库。另外,我有很多用户,他们的产品属于不同的用户。 因此,我需要编写一个具有多个条件的查询,其中搜索需要位于不同的表列中,因此它将是两个
->或where()
语句。但搜索需要找到属于同一用户的所有产品。但是,如果我有许多
->或where()
条件,query将开始在其他用户下搜索其他查询条件匹配的产品(“title”或“product\u number”)

那么,我如何更改我的代码,在那里我可以得到所有产品的搜索关键字下的产品在同一个用户

这是我的密码:

public function searchProduct(Request $search){
    $search_item = $search->search;
    $user = User::where('id', Auth::user()->id)->first();
    $inventory = Inventory::where('client_id', $user->client_id)
                            ->where('product_number', 'like', "%{$search_item}%")
                            ->orWhere('title', 'like', "%{$search_item}%")
                            ->orWhere('other_product_numbers', 'like', "%{$search_item}%")
                            ->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
                            ->paginate(50);

    $cleint_currency = SettingsClientCurrency::where('client_id', $user->client_id)->first();

    $inventory_products = Inventory::where('client_id', $user->client_id)->with('orderInRecord')->get();
    $inventory_total = 0;
    foreach ($inventory_products as $product) {
        $ordersin = $product->orderInRecord;
        foreach ($ordersin as $orderin) {
            $inventory_total += $orderin['price'] * $orderin['units'];

        }
    }

    return view('layouts.inventory.inventory')
            ->with('inventory', $inventory)
            ->with('inventory_total', $inventory_total)
            ->with('cleint_currency', $cleint_currency)
            ->with('user', $user);
}

您可以在第二个where子句中使用带有回调函数的两个where子句。那会得到你想要的结果

$inventory = Inventory::where('client_id', $user->client_id)
                       ->where(function ($query) use ($search_item) {
                           $query->where('product_number', 'like', "%{$search_item}%")
                                 ->orWhere('title', 'like', "%{$search_item}%")
                                 ->orWhere('other_product_numbers', 'like', "%{$search_item}%");
                       })
                       ->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
                       ->paginate(50);

您可以在第二个where子句中使用带有回调函数的两个where子句。那会得到你想要的结果

$inventory = Inventory::where('client_id', $user->client_id)
                       ->where(function ($query) use ($search_item) {
                           $query->where('product_number', 'like', "%{$search_item}%")
                                 ->orWhere('title', 'like', "%{$search_item}%")
                                 ->orWhere('other_product_numbers', 'like', "%{$search_item}%");
                       })
                       ->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
                       ->paginate(50);