具有动态约束的 Laravel Eager Load [英] Laravel Eager Load with dynamic constraints

查看:26
本文介绍了具有动态约束的 Laravel Eager Load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我理解为什么下面的代码有效

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 在这里未定义.你需要在闭包中添加 use 来使用 if,所以代码应该是这样的:

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 Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆