Laravel 函数内的未定义变量
Laravel undefined variable inside a function
我从 url 得到了我的 $id
,但我找不到在函数中使用它的方法。如何将 $id 传递给查询函数?
我尝试了 global $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);
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
。
我从 url 得到了我的 $id
,但我找不到在函数中使用它的方法。如何将 $id 传递给查询函数?
我尝试了 global $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);
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
。