简体   繁体   English

我必须使用 Eloquent 在 Laravel 中加入 6 个不同的表格

[英]I have to join 6 different tabel in Laravel using Eloquent

I have 6 different tables in my database and I have to fetch value from column type, id, title, created_at, updated_at, imported, import_url, cover_type, profile_image from all 6 tables and some extra values from column start_date and location from meetup table, job_location and cmp_name from job table and start_date and location from event table.我的数据库中有 6 个不同的表,我必须从所有 6 个表中的列类型、id、标题、created_at、updated_at、imported、import_url、cover_type、profile_image 以及来自列 start_date 的一些额外值和来自 meetup 表的位置获取值,作业表中的 job_location 和 cmp_name 以及事件表中的 start_date 和 location。 I am using union() function but it is not working as to join select statement from different table using union we should have same number of columns.我正在使用 union() function 但它无法使用联合从不同的表加入 select 语句,我们应该有相同数量的列。

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'), DB::raw('DATE_FORMAT(start_date, "%d %b %Y") as start_date'), 'imported', 'import_url', 'cover_type', 'profile_image', 'location')
        ->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', 'job_location', 'cmp_name')
        ->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'), DB::raw('DATE_FORMAT(start_date, "%d %b %Y") as start_date'), 'imported', 'import_url', 'cover_type', 'profile_image', 'location')
        ->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)
        ->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").'type_image/thumb/'.$value->profile_image;
                }
                $u_activity[$key]->post_url = url('/') . '/view-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);
}

I had the issue same as you are having.我遇到了和你一样的问题。

Keep the same columns as it is.保持与原来相同的列。

  • type, id, title, created_at, updated_at, imported, import_url, cover_type, profile_image类型、ID、标题、created_at、updated_at、导入、import_url、cover_type、profile_image

However the extra column you have, those need to concat in to one column with some unique separation sign like "," or ":" whatever fits best in your case.但是,您拥有的额外列需要使用一些独特的分隔符号(例如“,”或“:”)连接到一列中,以最适合您的情况。

Here is an example you can use as reference.这是一个可以用作参考的示例。

$u_education = DB::table('mst_education as education')
        ->select('type', 'id', 'title', 'created_at', 'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image',DB::raw('CONCAT("extra_column_1",",","extra_column_2") as extra_col'))
        ->where('user_id_fk',$user_id)
        ->where('status', '=', '1');
        $u_activity= DB::table('mst_event as event')
        ->select('type', 'id', 'title', 'created_at', 'updated_at', 'imported', 'import_url', 'cover_type', 'profile_image',DB::raw('CONCAT("extra_column_1",",","extra_column_2") as extra_col'))
        ->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)
        ->get();

Now when you get result, process them through foreach loop and separate those column by using "," separation we used.现在,当您获得结果时,通过 foreach 循环处理它们并使用我们使用的“,”分隔这些列。

Here is example for reference.这是供参考的示例。

$results = [];
if (sizeof($u_education) > 0)
{
    foreach ($u_education as $key => $value) {
        $results[$key][] = $value->id;
        $extra_col = explode("," , $value->extra_col);
        $results[$key]['extra_column_1'] = $extra_col[0];
        $results[$key]['extra_column_2'] = $extra_col[1];
    }
}

Hope this solution is work for you.希望此解决方案对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM