【问题标题】:I have to join 6 different tabel in Laravel using Eloquent我必须使用 Eloquent 在 Laravel 中加入 6 个不同的表
【发布时间】:2019-10-09 15:54:01
【问题描述】:

我的数据库中有 6 个不同的表,我必须从所有 6 个表中的列类型、id、标题、created_at、updated_at、imported、import_url、cover_type、profile_image 以及 start_date 列和 location 中的一些额外值中获取值工作表中的聚会表、job_location 和 cmp_name 以及事件表中的 start_date 和 location。我正在使用 union() 函数,但它不能使用联合从不同的表中加入 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);
}

【问题讨论】:

  • 您期望的结果是什么?也许使用集合方法可能比使用 sql unions 更简单

标签: php laravel eloquent eloquent-relationship


【解决方案1】:

我遇到了和你一样的问题。

保持与原来相同的列。

  • 类型、id、标题、created_at、updated_at、导入、import_url、cover_type、profile_image

不管你有多少额外的列,那些需要用一些独特的分隔符号连接到一个列中,比如“,”或“:”,只要最适合你的情况。

这是一个可以作为参考的例子。

$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();

现在,当您获得结果时,通过 foreach 循环处理它们,并使用我们使用的“,”分隔这些列。

这里是示例供参考。

$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];
    }
}

希望这个解决方案对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 2019-10-06
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多