user379888
user379888

Reputation:

How to join data in two variables

$orders_details and $orders have data from models. I am trying to join both using a common primary key (id). This is what I have tried,

    $orders_details = OrderDetails::
    where('SKU','LIKE','ROUTEINS%')
    ->limit(10)
    ->get();
    // order
    $orders = Order::limit(10)
    ->get();
    // return
    $merged = $orders_details->merge($orders);

The page stops loading when I do so

Upvotes: 0

Views: 89

Answers (1)

Patrick Schocke
Patrick Schocke

Reputation: 1491

You can use Laravel Relationships for this.

If your OrderDetails Table has a order_id column that references the order:

class Order extends Model {
    public function OrderDetail() {
        return $this->hasOne(OrderDetail::class);
    }
}

class OrderDetail extends Model {
   public function Order() {
       return $this->belongsTo(Order::class);
   }
}

Than you can get your order details with the corosponding order like this:

$orders_details = OrderDetails::
    where('SKU','LIKE','ROUTEINS%')
    ->limit(10)
    ->with('order')
    ->get();

Upvotes: 1

Related Questions