Laravel 5.1:查询生成器无法读取参数或值


Laravel 5.1 : argument or values not readable by Query Builder

这是我的route.php

Route::get('tempitems/{tempId}/add/{q?}', 
['uses' => 'ItemTemplateController@addItems', 'as' => 'tempitems.add']);

这是addItems方法

public function addItems(Request $request, $tempId)
{
 echo ($tempId) // 1 st echo
 $temp = DB::table('auto_items')
            ->join('items', 'auto_items.item_id', '=', 'items.id')
            ->select(
                'items.name',
                'auto_items.qty'
                )
            ->where('auto_items.template_id', '=' , $tempId)
            ->tosql();
  dd($temp);
  echo $tempId; // 2nd echo
  return view('template_items.add_items', [
      'active'    => $this->active,
      'items'     => $items,
      'template'  => $template,
      'page'      => $page,
      'success'   => $success,
      'tempItems' => $tempItems
    ]);  
}

当我访问localhost:8000/tempitems/1/add 时,查询生成器无法读取$tempId

1st echo => return 1
dd return => 
"select `items`.`name`, `auto_items`.`qty` from `auto_items` inner join `items` on 
`auto_items`.`item_id` = `items`.`id` where `auto_items`.`template_id` = ?"
2nd echo returned 1

我知道QueryBuilder使用get()来返回行,因为它返回了一个零数组/[];这就是我使用toSql()的原因。但是,查询生成器无法读取$tempId

编辑:在编辑之前,我正在使用

->where('auto_items.template_id', '=' , 1)

上面的代码仍然返回"?"

->where('auto_items.template_id', '=' , $tempId)

这也是返回的"?"

如果你想确保你插入的变量是否处理到查询中,你最好使用DB::getQueryLog(),它仍然在查询中显示?,但在bindings 中显示你的变量

    DB::enableQueryLog();
    $temp = DB::table('tabel')
        ->where('id', '=' , 1)
        ->get();
    return json_encode(DB::getQueryLog());

样本结果:

    [{"query":"select * from `age` where `id` = ?","bindings":[1],"time":0.19}]