【发布时间】: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