Laravel 4 用Laravel的雄辩ORM过滤查询
我定义了如下雄辩的模型: 阶级秩序的延伸是雄辩的{ 公共功能站{ 返回$this->belongsTo'Station'; } } 班站延伸雄辩{ 公共功能客户端{ 返回$this->belongsTo'Client'; } 公共职能令{ return$this->hasMany'Order'; } } 类客户端扩展了雄辩的{ 公共功能站{ return$this->hasMany'Station'; } } 现在我想做一个与所有这些问题相关的查询,但我很难想出如何用雄辩的方式来做。这是我要返回的查询的SQL: 选择o.id,c.name作为cname,s.name作为sname,o.created,o.due,o.name,o.comments,o.points 从命令 在o.station\u id=s.id上连接站s 在c.id=s.client\u id上加入客户机c s.id在哪里 从完成的订单中选择idLaravel 4 用Laravel的雄辩ORM过滤查询,laravel-4,eloquent,Laravel 4,Eloquent,我定义了如下雄辩的模型: 阶级秩序的延伸是雄辩的{ 公共功能站{ 返回$this->belongsTo'Station'; } } 班站延伸雄辩{ 公共功能客户端{ 返回$this->belongsTo'Client'; } 公共职能令{ return$this->hasMany'Order'; } } 类客户端扩展了雄辩的{ 公共功能站{ return$this->hasMany'Station'; } } 现在我想做一个与所有这些问题相关的查询,但我很难想出如何用雄辩的方式来做。这是我要返回
我不知道我是如何做到这一点的。Laravel/雄辩的方式。好吧,这里有几种处理方法 执行整个原始查询 提出雄辩的问题 为了获得对查询的更多控制,我将获得第一点 这可以通过直接执行语句来实现 DB::selectDB::raw$your\u sql\u此处 或者通过使用查询构造函数进行构建 第二种方法是研究关系是如何运作的。 最后的查询可能看起来不准确,但会给您一个提示:
Order::with('station.client')->whereIn('station.id', DB::raw('SELECT id FROM orders WHERE complete'))
希望这会有所帮助。此外,还有一个很好的解决方案,可以为您提供超过DB和模型对象的更多方法。您应该能够使用以下方法来实现这一点: 然后您可以在每个订单上循环:
foreach ($orders as $order) {
echo e("The order ID is $order->id <br>");
echo e("The station ID is {$order->station->id} <br>");
echo e("The client ID is {$order->station->client->id} <br>");
}
希望有帮助。您需要什么结果?雄辩的模型或这些选定领域的阵列?你确定s.id在哪里是不是在…里面?您可以使用query\Builder构建完全相同的查询,无需原始片段。
foreach ($orders as $order) {
echo e("The order ID is $order->id <br>");
echo e("The station ID is {$order->station->id} <br>");
echo e("The client ID is {$order->station->client->id} <br>");
}