Laravel Eager加载动态约束 [英] Laravel Eager Load with dynamic constraints
问题描述
任何人都请帮我明白为什么下面的代码是工作的
Can anybody please help me to understand why the following code is working
$x = $widget->objGallery->galleryItems()->with(array('captions' => function($query){ $query->where('locale', 'IT' );}))->get() ;
但是当我使用动态值
$id='11';
$x = $widget->objGallery->galleryItems()->with(array('captions' => function($query){ $query->where('locale', $id );}))->get() ;
表示
方法Illuminate\View\View :: __ toString()不得抛出异常
Method Illuminate\View\View::__toString() must not throw an exception
推荐答案
事实上很难说,因为你没有在这里展示相关代码,但是代码的问题:
In fact it's hard to say because you haven't showed here relevant code, but the problem with code:
$x = $widget->objGallery->galleryItems()->with(array('captions' =>
function($query){ $query->where('locale', $id );
}))->get();
是变量 $ id
未定义。您需要添加使用
才能在关闭时使用,因此代码应如下所示:
is that variable $id
is undefined here. You need to add use
to use if in closure, so the code should look like this:
$x = $widget->objGallery->galleryItems()->with(array('captions' =>
function($query) use($id) { $query->where('locale', $id );
}))->get();
您应该将环境更改为本地并打开调试,可能您会知道这个问题。可能是纠正这段代码时,我显示你不会有错误。
You should change your environment to local and have turned on debugging, probably you would then know about this problem. Probably when correcting this code as I showed you won't have the error.
这篇关于Laravel Eager加载动态约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!