laravel 5.3 查询生成器中的内部查询

inner query in laravel 5.3 query builder

我尝试使用 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 不为空,它显示错误变量未定义。

你必须像

一样使用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();

您必须使用 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();

试试这个,你必须像这样用 use 传递你的 $variable after 函数

$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();
$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();

您忘记使用 use

您需要使用 use$seller_name 传递给闭包。不要在闭包查询中使用 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();

您必须在函数中传递变量 使用 ($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();

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