Php 如何将此sql查询写入laravel eloquent?
我有一个查询,我正试图用它来优化我的旧查询,因为它们效率不够,但我很难找到有说服力的备忘单 我的问题是: 在o.id=t.oid上选择*从o内部连接t,其中t.wid位于(1,2,3,4)您必须阅读 您可以通过一个简单的关系来实现:Php 如何将此sql查询写入laravel eloquent?,php,mysql,sql,laravel-4,Php,Mysql,Sql,Laravel 4,我有一个查询,我正试图用它来优化我的旧查询,因为它们效率不够,但我很难找到有说服力的备忘单 我的问题是: 在o.id=t.oid上选择*从o内部连接t,其中t.wid位于(1,2,3,4)您必须阅读 您可以通过一个简单的关系来实现: class Transaction extends Eloquent { /** * Return the relationship **/ public function order(){ // Th
class Transaction extends Eloquent {
/**
* Return the relationship
**/
public function order(){
// The Transaction belongs to Order.
// So, the Transaction has a order_id reference
return $this->belongsTo('Order');
}
}
class Order extends Eloquent {
/**
* Return the relationship
**/
public function transaction(){
// Define the relationship. Order has one Transaction
// So, the table Transaction has a order_id reference.
return $this->hasOne('Transaction');
}
}
/**
/* In your controller:
**/
// Return the Transaction where id is 1, 2, 3 or 4.
$transaction = Transaction::whereIn('waypoint_id', array(1,2,3,4) )->get();
// Get the Order owner of the Transaction
$order = $transaction->order;
echo $order->id; // It prints the Order id
echo $transaction->id; // It prints the Transaction id
您还可以从订单
获取交易
,因此:
$order = Order::find(1); // Get the order with id 1
$transaction = $order->transaction; // Get the transaction
试试这个
$output = DB::table('orders as od')
->join('transactions as tr', 'od.id', '=', 'tr.order_id')
->whereIn('tr.waypoint_id', array(1,2,3,4))
->get();
print_r($output); // it will return an array of object
参考资料:请为您标记正确答案:)。