这是我获取两个对象的控制器代码。在我使用的每个对象的末尾->get();这显示了错误未定义的变量。但当我使用->first()时,它工作正常,但只显示第一条记录。但我需要完整的记录。

public function ratingdata()
{
      $pacra['pacra'] = DB::table('og_ratings AS p')

           ->select('p.client_id as Id','p.ppl_date as Ppl','p.notification_date as Notification ',
            'p.dissemination_date as Dissemination', 'p.rating_type_id as ratingTypeId',
            'p.rating_scale_id',  'p.pacra_action as Action' , 'og_companies.name as Entity',
            'og_main_sectors.title as Industry', 'p.detail_report as DetailReport',

            )
      ->get();




       $jcr['jcr'] = DB::table('og_ratings_jcr as t')

               ->select('t.client_id as id',
                't.jcr_dissemination_date as Dissemination',
                't.rating_type_id as ratingTypeId',
                't.rating_scale_id',
                't.jcr_action as Action' , 'og_companies.name as Entity',
                't.press_release_jcr as PressRelease',
                'og_actions.title as RatingAction',
                'og_outlooks.title as Outlook',
                'og_sterms.title as RatingST',
                'og_lterms.title as RatingLT',
                'og_segments.title',
                'og_rating_scales.title as RatingScale',
                'og_fund_types.title as fundtype',
                't.isActive as isActive' , 'og_main_sectors.title AS Industry' )
          ->get();

           return view('RatingsPacra', compact('pacra' , 'jcr'));
}

最佳答案

我发现您遗漏了表之间的关系,以及@apokryfos提到的额外的,
尝试以下方法:

public function ratingData()
{
    $pacra['pacra'] = \DB::connection()
        ->table('og_ratings')
        ->leftJoin('og_companies', 'og_companies.id', '=', 'og_ratings.og_companies_id')
        ->leftJoin('og_main_sectors', 'og_main_sectors.id', '=', 'og_ratings.og_main_sectors_id')
        ->select([
            'og_ratings.client_id as Id',
            'og_ratings.ppl_date as Ppl',
            'og_ratings.notification_date as Notification',
            'og_ratings.dissemination_date as Dissemination',
            'og_ratings.rating_type_id as ratingTypeId',
            'og_ratings.rating_scale_id',
            'og_ratings.pacra_action as Action',
            'og_companies.name as Entity',
            'og_main_sectors.title as Industry',
            'og_ratings.detail_report as DetailReport',
        ])
        ->get();

    $jcr['jcr'] = \DB::connection()
        ->table('og_ratings_jcr')
        ->leftJoin('og_actions', 'og_actions.id', '=', 'og_ratings_jcr.og_actions_id')
        ->leftJoin('og_outlooks', 'og_outlooks.id', '=', 'og_ratings_jcr.og_outlooks_id')
        ->leftJoin('og_sterms', 'og_sterms.id', '=', 'og_ratings_jcr.og_sterms_id')
        ->leftJoin('og_lterms', 'og_lterms.id', '=', 'og_ratings_jcr.og_lterms_id')
        ->leftJoin('og_segments', 'og_segments.id', '=', 'og_ratings_jcr.og_segments_id')
        ->leftJoin('og_rating_scales', 'og_rating_scales.id', '=', 'og_ratings_jcr.og_rating_scales_id')
        ->leftJoin('og_fund_types', 'og_fund_types.id', '=', 'og_ratings_jcr.og_fund_types_id')
        ->select([
            'og_ratings_jcr.client_id as id',
            'og_ratings_jcr.jcr_dissemination_date as Dissemination',
            'og_ratings_jcr.rating_type_id as ratingTypeId',
            'og_ratings_jcr.rating_scale_id',
            'og_ratings_jcr.jcr_action as Action', 'og_companies.name as Entity',
            'og_ratings_jcr.press_release_jcr as PressRelease',
            'og_actions.title as RatingAction',
            'og_outlooks.title as Outlook',
            'og_sterms.title as RatingST',
            'og_lterms.title as RatingLT',
            'og_segments.title',
            'og_rating_scales.title as RatingScale',
            'og_fund_types.title as fundtype',
            'og_ratings_jcr.isActive as isActive', 'og_main_sectors.title AS Industry',
        ])
        ->get();

    return view('RatingsPacra', compact('pacra', 'jcr'));
}

当您拥有外键时,应该在表之间更改它们。
您仍然可以继续使用表别名,我只是不喜欢使用它们,因为它们有时不能像在Laravel/Doctrine Dbal中预期的那样工作。
我还将方法名从ratingdata更新为ratingData,以符合PSR-1标准:
https://www.php-fig.org/psr/psr-1/
你也可以回到\DB:connection()->table(..DB::table(...,这只是一种固执己见的方式。

10-05 19:25