Laravel Ordering on Piviot table
public function get_user_documents() { $userId = Auth::id(); $user_documents = UserDocument::where('user_id', $userId) ->orWhereHas('share_documents', function ($query) use ($userId) { $query->where('users.id', $userId); }) ->with(['share_documents' => function ($query) use ($userId) { $query->where('users.id', $userId); }]) ->latest() ->get(); // $user_documents = $user_documents->map(function ($document) use ($userId) {…