У меня есть 6 разных таблиц в моей базе данных, и мне нужно извлечь значение из типа столбца, идентификатора, заголовка, create_at, updated_at, import, import_url, cover_type, profile_image из всех 6 таблиц и некоторых дополнительных значений из столбца start_date и местоположения изтаблица собраний, job_location и cmp_name из таблицы заданий, start_date и местоположение из таблицы событий. Я использую функцию union (), но она не работает, так как для объединения оператора select из другой таблицы с помощью union у нас должно быть одинаковое количество столбцов.
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);
}