doumouyi4039 2016-08-28 15:39
浏览 21

使用laravel 5.2中的数据透视表对记录进行排序

I need some help regarding sorting the record using pivot table in laravel 5.2.

models:

Product belongsToMany PremiumAdCategory
User hasMany Product

below is the relationship in Product model:

public function premium_ad(){
    return $this->belongsToMany('App\PremiumAdCategory', 'premiumadcategory_product', 'product_id', 'premiumadcategory_id')
                    ->withTimestamps()
                    ->withPivot(['coupon_id', 'total_amount', 'discount_amount', 'net_amount','payment_method'])
                    ->latest('pivot_premiumadcategory_id');
}

below is pivot table schema

Schema::create('premiumadcategory_product', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('premiumadcategory_id')->unsigned();
    $table->integer('product_id')->unsigned();
    $table->integer('coupon_id')->nullable()->default(0);
    $table->string('total_amount');
    $table->string('discount_amount');
    $table->string('net_amount');
    $table->string('payment_method');
    $table->timestamps();
    $table->foreign('premiumadcategory_id')->references('id')->on('premium_ad_categories')->onDelete('cascade');
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

I want to fetch all the records of a particular user from product table and sort them based on the pivot table. this is the code i use.

$product = Product::whereUserId(4)->with(['premium_ad' => function ($query) {
    $query->orderBy('pivot_premiumadcategory_id', 'DESC');
}])->get()->toArray();

But i can't able to sort the code, below is the dump query.the output of the query doesn't contain the sorted code.

select `premium_ad_categories`.*, `premiumadcategory_product`.`product_id` as `pivot_product_id`, `premiumadcategory_product`.`premiumadcategory_id` as `pivot_premiumadcategory_id`, `premiumadcategory_product`.`coupon_id` as `pivot_coupon_id`, `premiumadcategory_product`.`total_amount` as `pivot_total_amount`, `premiumadcategory_product`.`discount_amount` as `pivot_discount_amount`, `premiumadcategory_product`.`net_amount` as `pivot_net_amount`, `premiumadcategory_product`.`payment_method` as `pivot_payment_method`, `premiumadcategory_product`.`created_at` as `pivot_created_at`, `premiumadcategory_product`.`updated_at` as `pivot_updated_at` from `premium_ad_categories` inner join `premiumadcategory_product` on `premium_ad_categories`.`id` = `premiumadcategory_product`.`premiumadcategory_id` where `premiumadcategory_product`.`product_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) order by `pivot_premiumadcategory_id` desc

Please take a look and let me know what i am missing. Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思