mercredi 10 octobre 2018

Laravel Eager Loading with some where conditions in both ends

I am getting the sql query as this

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 < 2018-10-10 07:15:42 order by id desc limit 1

But what happened to the query i added wherenotnull of subscription_id? It's not coming in the query output. Give me a way to optimise the query and make it work.

    $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');
    }]);

And the GeoFilterActivation Model is

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');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire