从查询约束的关系中检索记录

时间:2017-12-12 14:27:01

标签: php laravel collections eloquent

我正在尝试从orders表中检索订单与supervisor相关联的所有记录。 OrdersSupervisorsbelongsToMany关系相关联,并拥有数据透视表。

所以我的代码看起来像这样:

$supervisor = User::where('phone_number', $request->msisdn)->first();
            $orders = Order::with(['supervisors' => function ($query) {
                $query->where('supervisor_id', $supervisor->id);
            }])->get();

我期待获得一系列订单,然后我会将其传递给通知但是得到:

[2017-12-12 14:25:27] local.ERROR: Undefined variable: supervisor   

{"exception":"[object] (ErrorException(code: 0): Undefined variable: supervisor 

不确定我做错了什么?

2 个答案:

答案 0 :(得分:3)

您需要使用USE将$ supervisor传递给函数

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />

    <!-- Reference to the Bing Maps SDK -->
    <script type='text/javascript'
            src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[your kep]' 
            async defer></script>
    
    <script type='text/javascript'>
    function GetMap()
    {
		var map = new Microsoft.Maps.Map('#myMap', {
			credentials: 'Your Bing Maps Key',
			mapTypeId: Microsoft.Maps.MapTypeId.road,			
			center: new Microsoft.Maps.Location(parseInt(document.getElementById("log")), parseInt(document.getElementById("lat"))),
			zoom: 10
		});
    }
    </script>
</head>
<body onload="GetMap();">
  
  <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
	
  Log: <input type="text" id="log" value="51.50632"><br>
  Lat: <input type="text" id="lat" value="-0.12714"><br>
  
</body>
</html>

答案 1 :(得分:0)

对于谷歌搜索类似的东西,我使用了它并且它起作用了:

$supervisor = User::where('phone_number', $request->msisdn)->first();
            $orders = Order::whereHas('supervisors', function ($query) use($supervisor) { 
                $query->where('supervisor_id', $supervisor->id); 
            })->where('status', '=', 'pending')->get();

非常感谢p.wright的帮助!