I have two models. There are just two different tables, and the only common is the order number.
class Order extends Model
{
protected $primaryKey = 'OrderId';
protected $keyType = 'string';
protected $table = 'Order';
public $timestamps = false;
public $incrementing = false;
public function crmOrder()
{
return $this->hasOne(CrmOrder::class, 'Order_Num', 'OrderId')
->select(['Account', 'OrderType']);
}
}
class CrmOrder extends Model
{
protected $primaryKey = 'Order_Num';
protected $keyType = 'string';
protected $table = 'CRMORDERS';
public $timestamps = false;
public $incrementing = false;
}
When I'm trying to query the following.
Order::with('crmOrder')->select(['OrderId', 'OrderCreatedDt'])->whereDate('OrderCreatedDt', '>', DB::raw('DATEADD(day,-21,GETDATE())'))->get();
I see two different queries. 1st is to model Order and 2nd to CrmOrder with ids from the 1st result. How to make just one query using JOIN between them?