みました!をcreated_atでソートしたい

成功

  • 最初はコントローラ側をイジっていたが、
  • withPivot

[参考資料:2軒] 1. belongsToMany な関係で中間テーブルのカラムでソートする方法

https://r17n.page/2019/11/01/php-laravel-orderby-intermediate-table/
  1. Laravel5.5 多対多の中間テーブルのカラムにorderBy条件で並び替えしたい。
https://error-search.com/error-post/detail/273/Laravel5.5%2B%25E5%25A4%259A%25E5%25AF%25BE%25E5%25A4%259A%25E3%2581%25AE%25E4%25B8%25AD%25E9%2596%2593%25E3%2583%2586%25E3%2583%25BC%25E3%2583%2596%25E3%2583%25AB%25E3%2581%25AE%25E3%2582%25AB%25E3%2583%25A9%25E3%2583%25A0%25E3%2581%25ABorderBy%25E6%259D%25A1%25E4%25BB%25B6%25E3%2581%25A7%25E4%25B8%25A6%25E3%2581%25B3%25E6%259B%25BF%25E3%2581%2588%25E3%2581%2597%25E3%2581%259F%25E3%2581%2584%25E3%2580%2582
class Post extends Model
{
    public function favorite_users()
    {
        // return $this->belongsToMany('App\User','favorites','post_id','user_id')->withTimestamps();

        /**
         * [機能追加]
         * 2020.02.07
         * 見ましたを押した順番にソートする機能を実装
         */
        return $this->belongsToMany('App\User','favorites','post_id','user_id')
                    ->withPivot('created_at AS joined_at')
                    ->orderBy('joined_at', 'desc')
                    ->withTimestamps();
    }
}