mercredi 18 novembre 2020

Database Query taking long time and then failed in laravel 5.8

I am using laravel query which is taking long time and then failed. I tried many ways but nothing worked.

This is the query

$consignments = Consignment::where('customer_id', $invoice->customer_id)->doesnthave('invoice_charges')->get();

here is consignment model which is having Many relationships

<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class Consignment extends Model
{
    protected $table = 'consignments';
    /**
     * @var array
     */
    protected $guarded = [];

    /**
     * @var array
     */
    protected $hidden = [
        'created_at', 'updated_at'
    ];

    public function pickupConsignment()
    {
        return $this->hasOne('App\Models\Admin\PickupDeliveryConsignment', 'pickup_consignment_id');
    }
    public function deliveryConsignment()
    {
        return $this->hasOne('App\Models\Admin\PickupDeliveryConsignment', 'delivery_consignment_id');
    }

    public function customers()
    {
        return $this->belongsTo('App\Models\Admin\Customer', 'customer_id');
    }

    public function income_charge_schemes()
    {
        return $this->hasOne('App\Models\Admin\IncomeChargeScheme', 'consignment_id');
    }

    public function expense_charge_schemes()
    {
        return $this->hasOne('App\Models\Admin\ExpenseChargeScheme', 'consignment_id');
    }


    public function delivery_addresses()
    {
        return $this->hasOne('App\Models\Admin\Address', 'id', 'delivery_address');
    }

    public function pickup_addresses()
    {
        return $this->hasOne('App\Models\Admin\Address', 'id', 'pickup_address');
    }

    public function product_types()
    {
        return $this->hasOne('App\Models\Admin\ProductType', 'id', 'product_type_id');
    }

    public function delivery_runs()
    {
        return $this->belongsTo('App\Models\Admin\DeliveryRun', 'delivery_run_id');
    }

    
    public function manifests()
    {
        return $this->belongsToMany('App\Models\Admin\Manifest');
    }

    public function run_sheets()
    {
        return $this->belongsToMany('App\Models\Admin\RunSheet');
    }

    public function consignment_manifest()
    {
        return $this->hasOne('App\Models\Admin\ConsignmentManifest', 'consignment_id');
    }

    public function consignment_run_sheet()
    {
        return $this->hasOne('App\Models\Admin\ConsignmentRunSheet', 'consignment_id');
    }

    public function sale_orders()
    {
        return $this->hasOne('App\Models\Admin\SaleOrder', 'id', 'sale_order_id');
    }

    public function charges()
    {
        return $this->hasMany('App\Models\Admin\Charge', 'object_id')->where('model', 'Consignment');
    }

    public function invoice_charges()
    {
        return $this->hasOne('App\Models\Admin\InvoiceCharge', 'object_id')->where('model', 'Consignment');
    }

    public function delivery_address_strings()
    {
        return $this->belongsTo('App\Models\Admin\AddressString', 'delivery_address_string_id');
    }

    public function pickup_address_strings()
    {
        return $this->belongsTo('App\Models\Admin\AddressString', 'pickup_address_string_id');
    }

    public function  consignment_adhoc_charge_data()
    {
        return $this->hasMany('App\Models\Admin\AdhocChargeData', 'object_id')->where('model', 'Consignment');
    }

    public function  consignment_invoices()
    {
        return $this->hasMany('App\Models\Admin\ConsignmentInvoice', 'consignment_id');
    }

    public function  consignment_charge_errors()
    {
        return $this->hasMany('App\Models\Admin\ChargeError', 'object_id')->where('model', 'Consignment');
    }

    public function  consignment_errors()
    {
        return $this->hasMany('App\Models\Admin\Error', 'object_id')->where('model', 'App\Models\Admin\Consignment');
    }


    public function vendorConsignments()
    {
        return $this->hasMany('App\Models\Admin\VendorConsignment', 'consignment_id');
    }

    public function ConsignmentCod()
    {
        return $this->hasMany('App\Models\Admin\CashOnDeliveryConsignment', 'consignment_id');
    }
}

As you see consignment model has many relationships but i think query fetch all the relationships data with it .

When I use toBase() It gets load quickly but it doesn't fetch relationship which is required

here is the query with toBase()

$consignments = Consignment::where('customer_id', $invoice->customer_id)->doesnthave('invoice_charges')->toBase()->get();

I think this relationship which is creating issue

I also tried cursor() function but didn't work ..

How i can optimize this query any suggestion?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire