如何从控制器中获取两个对象的结果,以便在Laravel5中查看

时间:2018-03-22 07:37:26

标签: php mysql laravel

这是我的控制器代码,用于获取两个对象。在我使用的每个对象的末尾 - > 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'));
}

1 个答案:

答案 0 :(得分:1)

我发现你错过了表格之间的关系,以及@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(...,这只是一种自以为是的方式。