【问题标题】:Laravel Eager Loading with some where conditions in both endsLaravel Eager Loading 两端都有一些 where 条件
【发布时间】:2019-03-15 01:10:14
【问题描述】:

我得到这样的 sql 查询:

select * from geofilter_activations where not exists (select * from snap_submissions where geofilter_activations.purchase_id = snap_submissions.purchase_id) and status = 0 and created_at id desc limit 1

但是我添加了 wherenotnull 的 subscription_id 的查询发生了什么? 它没有出现在查询输出中。给我一个方法来优化查询并让它工作。

    $time_before = '10';
    $dt = Carbon::now();

    $activation = GeofilterActivation::doesntHave('submission_data')
        ->where('status', '0')
        ->where('created_at', '<', $dt->subMinutes($time_before)->toDateTimeString())
        ->OrderBy('id','desc')
        ->first();

    $activation->load(['purchase' => function ($query) {
        $query->whereNotNull('subscription_id');
    }]);

GeoFilterActivation 模型是:

<?php

namespace App\Models\Geofilter;

use Illuminate\Database\Eloquent\Model;

class GeofilterActivation extends Model
{
    public function purchase(){
        return $this->belongsTo('App\Models\Geofilter\GeofilterPurchase', 'purchase_id');
    }
    public function submission_data(){
        return $this->belongsTo('App\Models\Geofilter\SnapSubmission', 'purchase_id', 'purchase_id');
    }

    public function ad_stat(){
        return $this->belongsTo('App\Models\Geofilter\SnapAdStat', 'purchase_id', 'purchase_id');
    }

    public function currency(){
        return $this->belongsTo('App\Models\Currency', 'currency_code', 'code');
    }
}

【问题讨论】:

  • 你是如何“获取 sql 查询”的?

标签: mysql laravel laravel-5 eloquent


【解决方案1】:

也许这些方面的东西可以帮助你:

$result = GeofilterActivation::with(['purchase' => function($query) {
        $query->whereNotNull('subscription_id');
    }])
    ->doesntHave('submission_data')
    ->where('status','0')
    ->where('created_at', '<', $dt->subMinutes($time_before)->toDateTimeString())
    ->OrderBy('id','desc')
    ->first();

您的查询不包含load 的原因是因为load 在您的查询执行后被调用,因此您必须使用with 在查询中包含该关系。您上面提到的负载示例实际上是lazy load,因为您在需要时请求对象。

【讨论】:

  • 当我这样给出时,我得到这样的查询:** select * from geofilter_activations where not exists (select * from snap_submissions where geofilter_activations.purchase_id = snap_submissions .purchase_id) and status = 0 and created_at id desc limit 1) **
  • 如果你改变链式方法的顺序怎么办,尝试先调用with然后doesntHave,我已经更新了答案
  • 仍然错误@Nikola Gavric $result = GeofilterActivation::with(['purchase' => function($query) { $query->whereNotNull('subscription_id'); }])->doesntHave ('submission_data')->where('status','0') ->where('created_at1', 'subMinutes($time_before)->toDateTimeString())->OrderBy('id ','desc')->first(); select * from geofilter_activations` 不存在的地方(从snap_submissions 中选择* geofilter_activations.purchase_id = snap_submissions.purchase_id)和status = 0 和created_at id desc 限制 1)`
  • 您需要submission_data 做什么?您可以将其从查询中删除还是很重要?
  • 是的,这很关键,只有当submission_data表为空时才有条件获取数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2013-03-09
  • 2021-10-17
  • 1970-01-01
  • 2018-01-10
  • 2017-08-19
  • 1970-01-01
相关资源
最近更新 更多