Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

0
Major performance issue with laravel
Post Body

I have a laravel app with an Order Model that HasMany OrderLines, and HasMany OrderAddresses.

In order to save an Order I run this function, which works great, but uses 9 queries to run. Can you help me figure out how to run this faster.

  public function save(array $options = array()) {
    $exists = static::where('OrderID', '=', $this->OrderID)->first();

    if( ! $exists) {
      parent::save($options);
      $exists = static::where('OrderID', '=', $this->OrderID)->first();
    }else {
      Log::info("Updating already inserted record with OrderID = $this->OrderID");
      parent::where('OrderID','=',$this->OrderID)->update($this->getAttributes());
    }

    if(isset($this->addresses)) {
      foreach($this->addresses as $a) {
        $a -> order_id = $exists->id;
        if($a -> AddressType == 'ShippingInfo') {
          if($s = $exists->shippingAddress()->first()) {
            $s->update($a->getAttributes());
          }else {
            $a->save();
          }
        }
      }
      foreach($this->addresses as $a) {
        $a -> order_id = $exists->id;
        if($a -> AddressType == 'BillingInfo') {
          if($b = $exists->billingAddress() -> first()) {
            $b->update($a->getAttributes());
          }else {
            $a->save();
          }
        }
      }
    }
    if(isset($this->orderLines) && sizeof($this->orderLines) > 0 ) {
      foreach($this->orderLines as $ord) {
        $ord -> order_id = $exists->id;
        if($ol = OrderLine::where('LineItemID','=', $ord -> LineItemID)->first()) {
          foreach($ord->getAttributes() as $attr => $val) {
            $ol -> $attr = $val;
          }
          $ol -> save();
        }else {
          $ord->save();
        }
      }
    }
  }

Author
Account Strength
90%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,141
Link Karma
110
Comment Karma
4,031
Profile updated: 1 week ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
11 years ago