laravel 5.3查询构建器中的内部查询

时间:2017-05-26 06:29:51

标签: laravel laravel-query-builder inner-query

我尝试使用Laravel内部查询来获取数据,但我遇到了一个问题,我将值变量作为值传递,我的查询如下所示。

    $seller_name = $request->seller_name;
    $supplier_address = DB::table('supplier_addresses')
        ->where('company_main_name', function($query)
        {
            $query->select('company_main_name')
                ->where('company_name',$seller_name)
                ->from('supplier_addresses')
                ->get();
        })
        ->get();

如果我传递的是静态值而不是$seller_name,那么它工作正常,我检查$seller_name不为空,它显示错误变量未定义。

6 个答案:

答案 0 :(得分:4)

您必须在功能使用($ seller_name)

中传递变量
$seller_name = $request->seller_name; 
$supplier_address = 
    DB::table('supplier_addresses')
       ->where('company_main_name', function($query) use ($seller_name) 
       {
              $query->select('company_main_name')
                    ->where('company_name',$seller_name) 
                    ->from('supplier_addresses') ->get(); 
       }) 
    ->get();

你可以在这里参考doc: https://laravel.com/docs/5.4/queries#conditional-clauses

答案 1 :(得分:1)

您必须使用use之类的

$seller_name = $request->seller_name;
$supplier_address = DB::table('supplier_addresses')
    ->where('company_main_name', function($query) use ($seller_name)
    {
        $query->select('company_main_name')
            ->where('company_name',$seller_name)
            ->from('supplier_addresses')->get();
    })
    ->get();

答案 2 :(得分:1)

您必须使用use ($seller_name)

将变量传递给匿名函数
$seller_name = $request->seller_name;
$supplier_address = DB::table('supplier_addresses')
    ->where('company_main_name', function($query) use ($seller_name)
    {
        $query->select('company_main_name')
            ->where('company_name',$seller_name)
            ->from('supplier_addresses')
            ->get();
    })
    ->get();

答案 3 :(得分:1)

试试这个,你必须在use这样的函数之后传递你的$变量

$seller_name = $request->seller_name;
$supplier_address = DB::table('supplier_addresses')
        ->where('company_main_name', function($query ) use($seller_name)
        {
            $query->select('company_main_name')
                ->where('company_name',$seller_name)
                ->from('supplier_addresses')
                ->get();
        })
        ->get();

答案 4 :(得分:1)

def normalize(df):
    result = df.copy()
    for feature_name in df.columns:
        max_value = df[feature_name].max()
        min_value = df[feature_name].min()
        result[feature_name] = (df[feature_name] - min_value) / (max_value - min_value)
    return result

df_normalized = normalize(df).reset_index().sort_index(axis=1)
print (df_normalized.head())
         0  1         2         3         4         5         6         7   \
0  0.000915  M  0.521037  0.022658  0.545989  0.363733  0.593753  0.792037   
1  0.000915  M  0.643144  0.272574  0.615783  0.501591  0.289880  0.181768   
2  0.092495  M  0.601496  0.390260  0.595743  0.449417  0.514309  0.431017   
3  0.092547  M  0.210090  0.360839  0.233501  0.102906  0.811321  0.811361   
4  0.092559  M  0.629893  0.156578  0.630986  0.489290  0.430351  0.347893   

         8         9     ...           22        23        24        25  \
0  0.703140  0.731113    ...     0.620776  0.141525  0.668310  0.450698   
1  0.203608  0.348757    ...     0.606901  0.303571  0.539818  0.435214   
2  0.462512  0.635686    ...     0.556386  0.360075  0.508442  0.374508   
3  0.565604  0.522863    ...     0.248310  0.385928  0.241347  0.094008   
4  0.463918  0.518390    ...     0.519744  0.123934  0.506948  0.341575   

         26        27        28        29        30        31  
0  0.601136  0.619292  0.568610  0.912027  0.598462  0.418864  
1  0.347553  0.154563  0.192971  0.639175  0.233590  0.222878  
2  0.483590  0.385375  0.359744  0.835052  0.403706  0.213433  
3  0.915472  0.814012  0.548642  0.884880  1.000000  0.773711  
4  0.437364  0.172415  0.319489  0.558419  0.157500  0.142595  

[5 rows x 32 columns]

您忘记使用$seller_name = $request->seller_name; $supplier_address = DB::table('supplier_addresses') ->where('company_main_name', function($query ) use($seller_name) { $query->select('company_main_name') ->where('company_name',$seller_name) ->from('supplier_addresses') ->get(); }) ->get();

答案 5 :(得分:0)

您需要使用$seller_nameuse传递给闭包。不要在闭包查询中使用get()来通过单个数据库查询获得结果。

$supplier_address = DB::table('supplier_addresses')
    ->where('company_main_name', function($query) use ($seller_name) {
        $query->select('company_main_name')
            ->where('company_name', $seller_name)
            ->from('supplier_addresses');
    })
    ->get();

此外,您的代码与此相同。

$supplier_address = DB::table('supplier_addresses')
    ->where('company_name', $seller_name)
    ->get();