【问题标题】:Query two tables with Laravel and two WHERE conditions使用 Laravel 和两个 WHERE 条件查询两个表
【发布时间】:2016-10-19 03:04:12
【问题描述】:

我有桌子orders 和桌子payments。我想查询表orders,加入表payments,并显示一个订单是否已付款。

这是订单模型

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

     public function paidorders() {
         return $this->hasMany('payments', 'processed');
     }
}

这是付款模式

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

     public function orders()
     {
         return $this->hasMany('Order', 'user_id');
     }
}

还有用户模型

public function orders() {
    return $this->hasMany('Order', 'user_id');
}

这就是我仅显示当前订单的方式,没有状态已付款/未付款。

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

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

SELECT orders. * , payments. * 
FROM orders
   INNER JOIN payments ON orders.user_id = payments.userID
WHERE orders.user_id =2
AND payments.userID =2

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

我不知道如何构建这个查询

更新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) } } }

【问题讨论】:

  • 付款有很多订单?这不应该属于秩序吗?
  • 可能是的.. 但我仍然不太擅长 Laravel 并且不了解如何构建查询

标签: php mysql laravel laravel-4


【解决方案1】:
$query = Order::select(DB::Raw('payments.processed'))
    ->join('payments', 'orders.order_id', '=', 'payments.orderID')
    ->where('orders.user_id',  2)
    ->where('payments.userID', 2)
    ->get();

【讨论】:

  • 我添加了订单转储。我可以在转储中看到["processed"]=> string(1) "1",这是数据库中的值
  • 请分享完整的转储结果
  • 这是$orders的完整转储
  • 必须是对象(Illuminate\Database\Eloquent\Collection)
【解决方案2】:
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();

【讨论】:

  • 我相信 op 想使用 Eloquent
  • 是的,有了这个我有'Call to undefined method stdClass::getOrderData()'
  • 是的,我收到此错误'Call to undefined method stdClass::getOrderData()'
  • Ok 数组以对象格式返回,您还检查了查询,只需将 ->get() 替换为 ->toSql()
  • 我不知道这是否重要,但我没有通过 url/button 传递用户 ID。我通过 csrf token..
猜你喜欢
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2021-12-06
  • 2020-12-26
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多