简体   繁体   中英

Laravel Eloquent Inner Join

This is the query I currently using for Inner Join in my laravel app:

public function getReservationListAPI()
    {
        $id = JWTAuth::parseToken()->authenticate()->id;

        $result = DB::table('pporders AS ord')
            ->join('products AS pd', 'ord.product_id', '=', 'pd.id')
            ->select('ord.*')
            ->where('pd.user_id',$id)
            ->get();
        dd($result);

    }

How can I wrote this query in Eloquent form? Thanks!!


EDIT

Relationship:

Product hasMany Order
Order belongsTo Product
User hasMany Product
Product belongsTo User

It depends on your relation, but something like this:

Pporders::with(['product' => function($q) use($id) {
    $q->where('user_id', $id);
}])->get();

But it won't make the same sql query what you described, but gives the same result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM