2017-05-26 65 views
0

我試圖使用Laravel內部查詢來獲取數據,但我面臨一個問題,我將value變量作爲值傳遞給我的查詢,如下所示。laravel中的內部查詢5.3查詢構建器

$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不爲空,就說明沒有定義錯誤變量。

+0

確保您從''獲得$ seller_name' $ seller_name = $ request-> seller_name;' –

+0

'$ seller_name'有一個值。 – HirenMangukiya

+0

@HirenMangukiya在'function'調用之後只需傳遞'use($ seller_name)'! –

回答

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

0

你需要傳遞$seller_name使用use關閉。請不要在關閉查詢中使用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(); 
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(); 

你可以在這裏參考文檔: https://laravel.com/docs/5.4/queries#conditional-clauses