Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将此sql查询写入laravel eloquent?_Php_Mysql_Sql_Laravel 4 - Fatal编程技术网

Php 如何将此sql查询写入laravel eloquent?

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

我有一个查询,我正试图用它来优化我的旧查询,因为它们效率不够,但我很难找到有说服力的备忘单

我的问题是:

在o.id=t.oid上选择*从o内部连接t,其中t.wid位于(1,2,3,4)

您必须阅读

您可以通过一个简单的关系来实现:

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

参考资料:

请为您标记正确答案:)。