Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据数据库中最新日期位于顶部的列排序日期_Php_Laravel_Eloquent - Fatal编程技术网

Php 如何根据数据库中最新日期位于顶部的列排序日期

Php 如何根据数据库中最新日期位于顶部的列排序日期,php,laravel,eloquent,Php,Laravel,Eloquent,我的数据库中有6个不同的表,在所有6个表中都有一列“created_at”。我想把我的回复排在最前面。我正在使用orderBy('created_at'),但它以错误的顺序返回帖子。我的数据库中有一个“created_at”列,类型为“timestamp”。它将按照以下顺序返回邮件:2019年10月1日至6日、2019年10月2日至7日、2019年9月3日至23日、2019年9月4日至26日、2019年9月5日至27日等。这是我的密码:- public function wsUserActivi

我的数据库中有6个不同的表,在所有6个表中都有一列“created_at”。我想把我的回复排在最前面。我正在使用orderBy('created_at'),但它以错误的顺序返回帖子。我的数据库中有一个“created_at”列,类型为“timestamp”。它将按照以下顺序返回邮件: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);
}

您的
orderby
需要降序,而不是默认的升序。我认为这比向查询中添加
DESC
要复杂一些。我认为问题在于
UNION
DATE\u格式(created_at
它确实是按created at列排序的,只是不作为日期。相反,我认为它是按字符串排序的。最简单的答案是尝试从所有联合主题中删除date_格式,然后用PHP进行格式化。如果你想用SQL进行格式化,你需要在从另一个SELECT的FROM子句,并在最外层的查询中执行DATE\u格式。我无法删除DATE\u格式。是否有其他方法订购itI我不理解。请在代码中更正后发送
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);
}