我从网址获取$id
,但我找不到在函数中使用它的方法。如何将$ id传递给查询函数?
我尝试了全球$ id,但我仍然无法获得值,只是空页。
我的控制器。
class BookController extends Controller
{
public function bookIndex($id, $slug)
{
// echo $id works without a problem
$findcover = Thing::with(array('cover' => function($query) {
$query->where('imageable_type', 'App\Models\Thing')
->where('imageable_id', $id);
}))->find($id);
$maincover = $findcover->cover;
}
错误:
ErrorException in BookController.php line 49:
Undefined variable: id
第49行
->where('imageable_id', $id);
答案 0 :(得分:12)
class BookController extends Controller
{
public function bookIndex($id, $slug) {
// echo $id works without a problem
$findcover = Thing::with(array('cover' => function($query) use ($id) {
$query->where('imageable_type', 'App\Models\Thing')
->where('imageable_id', $id);
}))->find($id);
$maincover = $findcover->cover;
}
添加保留字使用,在您的查询中插入$id
。