如何基于数据库中的列排序日期,最新日期在最前面

Shubham

我的数据库中有6个不同的表,所有6个表中都有一列“ created_at”。我想对我的回复进行排序,以使最新帖子排在最前面。我正在使用orderBy('created_at'),但返回的顺序错误。我的数据库中具有“ timestamp”类型的“ created_at”列。它将按此顺序退回该职位2019年10月1日至6日,2019年10月2日至7日,2019年9月3日至23日,2019年9月4日至26日,2019年9月5日至27日等等。这是我的代码:-

public function wsUserActivity(){
    $request = Input::all();
    try {
        $user_id = $request['user_id'];
        $no=isset($request['page_number'])?$request['page_number']:0;
        $nos=isset($request['count'])?$request['count']:10;
        $skp=$no*$nos;
        $array_json_return = array('status' => '1','msg' => 'Success');


        $u_activity = array();
        $u_article = DB::table('mst_article as article')
        ->select(DB::raw('"article" as type'),'id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');




        $u_meetup = DB::table('mst_meetup as meetup')
        ->select(DB::raw('"meetup" as type'),'id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image' )
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');



        $u_question = array();
        $u_question = DB::table('mst_question as question')
        ->select(DB::raw('"question" as type'),'id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');


        $u_job = array();
        $u_job = DB::table('mst_job as job')
        ->select(DB::raw('"job" as type'),'id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');



        $u_education = array();
        $u_education = DB::table('mst_education as education')
        ->select(DB::raw('"education" as type'),'id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');


        $u_activity= DB::table('mst_event as event')
        ->select(DB::raw('"event" as type'),'id','title', DB::raw('DATE_FORMAT(created_at, "%d %b %Y") as created_at'), DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image' )
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1')
        ->union($u_article)->union($u_question)->union($u_meetup)->union($u_job)->union($u_education)
        ->orderBy('created_at')
        ->skip($skp)
        ->take($nos)
        ->get();

        if (count($u_activity)>0){
            foreach ($u_activity as $key => $value){
                if (!empty($value->profile_image)){
                $u_activity[$key]->profile_image_url = config("feature_pic_url").$value->type.'_image/thumb/'.$value->profile_image;
                }
                $u_activity[$key]->post_url = url('/') . '/view-'.$value->type.  '/' . $value->id;
            }
        }



        $array_json_return['u_activity'] = $u_activity;


    } catch (\Exception $e) {
        $array_json_return = $this->api_default_fail_response(__function__, $e);
    }

    echo json_encode($array_json_return);
}
Shubham
public function wsUserActivity(){
    $request = Input::all();
    try {
        $user_id = $request['user_id'];
        $no=isset($request['page_number'])?$request['page_number']:0;
        $nos=isset($request['count'])?$request['count']:10;
        $skp=$no*$nos;
        $array_json_return = array('status' => '1','msg' => 'Success');


        $u_activity = array();
        $u_article = DB::table('mst_article as article')
        ->select(DB::raw('"article" as type'),'id','title',  'created_at',  'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');




        $u_meetup = DB::table('mst_meetup as meetup')
        ->select(DB::raw('"meetup" as type'),'id','title', 'created_at', 'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image' )
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');



        $u_question = array();
        $u_question = DB::table('mst_question as question')
        ->select(DB::raw('"question" as type'),'id','title', 'created_at', 'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');


        $u_job = array();
        $u_job = DB::table('mst_job as job')
        ->select(DB::raw('"job" as type'),'id','title','created_at',  'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');



        $u_education = array();
        $u_education = DB::table('mst_education as education')
        ->select(DB::raw('"education" as type'),'id','title',  'created_at', DB::raw('DATE_FORMAT(updated_at, "%d %b %Y") as updated_at'), 'imported', 'import_url', 'cover_type', 'profile_image')
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');


        $u_activity= DB::table('mst_event as event')
        ->select(DB::raw('"event" as type'),'id','title',  'created_at',  'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image' )
        ->selectRaw('SUBSTRING(`description`, 1, 200) as `description`')
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1')
        ->union($u_article)->union($u_question)->union($u_meetup)->union($u_job)->union($u_education)
        ->skip($skp)
        ->take($nos)
        ->latest()
        ->get();

        if (count($u_activity)>0){
            foreach ($u_activity as $key => $value){
                $u_activity[$key]->created_at = DATE_FORMAT(new \DateTime($value->created_at), "d M Y");
                $u_activity[$key]->updated_at = DATE_FORMAT(new \DateTime($value->updated_at), "d M Y");
                if (!empty($value->profile_image)){
                $u_activity[$key]->profile_image_url = config("feature_pic_url").$value->type.'_image/thumb/'.$value->profile_image;
                }
                $u_activity[$key]->post_url = url('/') . '/view-'.$value->type.  '/' . $value->id;
            }
        }



        $array_json_return['u_activity'] = $u_activity;


    } catch (\Exception $e) {
        $array_json_return = $this->api_default_fail_response(__function__, $e);
    }

    echo json_encode($array_json_return);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在MySQL中从数据库中选择最新日期

如何按日期排序并让未读邮件排在最前面?

如何现在用日期更新日期时间列,但时间在 MySQL 数据库中为负 2 小时

如何基于最新日期获取数据框的整个行

如何从多个表中按最新日期记录排序?

按 2 列的最新日期排序

如何从数据表中的最新日期和时间排序

如何从Firebase数据库更新日期2次?

如何找到mysql数据库的最后更新日期

在SQL数据库中插入并更新日期时间

如何从表中获取最新日期数据

如何从Laravel中的最新日期检索数据?

如何基于日期作为数据库中列的确切年份筛选用户?

如何从 MySQL Workbench 的两列中获取最新日期?

如何用列 (M) 中的最新日期替换值

如何使用Python中的最新日期条件检索列?

在列中查找最新日期

列中的值所需的最新日期

Laravel-查询数据库表上最新日期的优雅方法

如何在MySQL中首先使所选列的行排在最前面,然后使不属于所选列的其余行排在最前面?

如何根据日期组的最新日期过滤数据?

如何显示按日期排序和按日期分组的数据库中的JSON记录

如何显示按日期 $row 中的日期排序的数据库结果

访问SQL以最新日期对数据进行排序

根据 Firebase 的最新日期将数据排序到 RecyclerView

在PySpark日期列中获取每年的最新日期

根据列表添加新列,并按最新日期排序

如何在MySQL中按DMY格式从Mysql数据库中按日期列显示并基于它进行搜索

如何将jdiv中的div放在最前面?