Ich habe 6 verschiedene Tabel in Laravel verbinden eloquent mit

stimmen
-1

Ich habe 6 verschiedene Tabellen in meiner Datenbank und ich habe Wert aus Spalte Typ, ID, Titel, created_at, updated_at, importiert, import_url, cover_type, Profile_image von allen 6 Tischen und einigen zusätzlichen Werten aus der Spalte start_date und Lage von meetup Tisch zu holen, job_location und cmp_name von Jobtabelle und start_date und Lage von Ereignistabelle

          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);
}

Ich verwende Union () Funktion, aber es funktioniert nicht als Select-Anweisung aus verschiedener Tabelle verbinden Vereinigung mit uns gleiche Anzahl von Spalten haben sollten.

Veröffentlicht am 09/10/2019 um 18:54
quelle vom benutzer
In anderen Sprachen...                            


1 antworten

stimmen
0

Ich hatte das Problem gleiche wie Sie haben.

Halten Sie die gleichen Spalten wie es ist.

  • Typ, ID, Titel, created_at, updated_at, importiert, import_url, cover_type, Profile_image

Jedoch ist die zusätzliche Spalte, die Sie haben, müssen diejenigen mit einigen einzigartigen Trennzeichen einer Spalte verketten in wie „“ oder ‚‘, was am besten in Ihrem Fall paßt.

Hier ist ein Beispiel, das Sie als Referenz verwenden können.

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

Nun, wenn Sie Ergebnis, verarbeiten sie durch foreach-Schleife und trennen jene Spalte unter Verwendung „“ Trennung Gewöhnen wir.

Hier ist beispielsweise als Referenz.

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

Hoffen, dass diese Lösung für Sie arbeiten ist.

Beantwortet am 23/10/2019 um 13:36
quelle vom benutzer

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more