0
$orders = Order::where('status','=',2)
            ->with(array(
                'orderItems' => function($query) {
                    $query->whereNull('status');
                }
            ))->leftjoin('users', function($join)
   {
        $join->on('orders.user_id', '=', 'users.id');
   })
            ->get();

左結合を行うと、ユーザーの詳細を取得できますが、注文番号は User_ID(Order Table) になります。たとえば、->User_Id または ->Order_Id を取得しようとすると、同じになり、意味がありません。オーダー ID がユーザー ID のように重複しています。

ユーザー :: 注文が多い

これは左結合/結合でのみ発生します。これを修正する方法はありますか?

4

3 に答える 3

3

関係として定義する必要があります。

class User extends Eloquent {

    public function comments()
    {
        return $this->hasMany('Order');
    }
}

class Order extends Eloquent {

    public function comments()
    {
        return $this->belongsTo('User');
    }
}

それからする

$orders = Order::where('status','=',2)->with(array(
            'orderItems' => function($query) {
                $query->whereNull('status');
            }
        ));
echo 'this order belongs to user_id: '.$orders->user()->id;
于 2013-09-21T07:47:22.227 に答える