用 Laravel 和两个 WHERE 条件查询两个表

S.I*_*.I. 1 php mysql laravel laravel-4

我有桌子orders和桌子payments。我想查询 table orders,join tablepayments并显示订单已支付,哪些不是。

这是订单模型

class Order extends Eloquent {
     protected $table = 'orders';
     protected $primaryKey = 'order_id';

     public function paidorders() {
         return $this->hasMany('payments', 'processed');
     }
}
Run Code Online (Sandbox Code Playgroud)

这是付款模式

class Payment extends Eloquent {
     protected $table = 'payments';
     protected $primaryKey = 'paymentID';

     public function orders()
     {
         return $this->hasMany('Order', 'user_id');
     }
}
Run Code Online (Sandbox Code Playgroud)

和用户模型

public function orders() {
    return $this->hasMany('Order', 'user_id');
}
Run Code Online (Sandbox Code Playgroud)

这就是我只显示当前订单而没有支付/未支付状态的方式。

  $orders = self::$user->orders()->get();
     return View::make('site.users.orders', [
        'orders' => $orders
     ]);
Run Code Online (Sandbox Code Playgroud)

这是查询,但我不知道如何在 Laravel 中实现它

SELECT orders. * , payments. * 
FROM orders
   INNER JOIN payments ON orders.user_id = payments.userID
WHERE orders.user_id =2
AND payments.userID =2
Run Code Online (Sandbox Code Playgroud)

self::$user->...是登录的用户。在WHERE子句中如何使用 this ?

我不明白如何构建这个查询

更新 dd($orders)

object(Illuminate\Database\Eloquent\Collection)#264 (1) { ["items":protected]=> array(1) { [0]=> object(Order)#260 (20) { ["table":protected]=> string(6) "orders" ["primaryKey":protected]=> string(8) "order_id" ["connection":protected]=> NULL ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(1) { ["processed"]=> string(1) "1" } ["original":protected]=> array(1) { ["processed"]=> string(1) "1" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } } }
Run Code Online (Sandbox Code Playgroud)

小智 7

Try this query code:-

$users = DB::table('orders')
            ->join('payments', 'orders.user_id', '=', 'payments.userID')
            ->where('orders.user_id', '2')
            ->where('payments.userID', '2')
            ->select('orders.*', 'payments.*')
            ->get();
Run Code Online (Sandbox Code Playgroud)