未显示Laravel sql连接属性

时间:2015-06-18 12:01:09

标签: sql laravel-5

我正在laravel中进行sql连接,并面临一个问题,即表中的id未在结果中填充,而我可以在数据库中看到id。这是我正在运行的查询:

 $jobs = Job::leftJoin('messages', function($join)
                                 {
                                   $join->on('jobs.id', '=', 'messages.job_id')
                                         ->where('messages.sender_id', '=', getLoggedInId());
                                 })
           ->whereIn('category_id', $category_arr)
           ->where('status', 1)
           ->where('request_for', 0)
           ->groupBy('jobs.id')
           ->orderBy('messages.created_at', 'DESC')
           ->paginate(10);

$output = print_r($jobs,1);           
Log::info('jobs : '.$output);

日志打印为:

  [2015-06-18 16:56:27] local.INFO: jobs : Illuminate\Pagination\LengthAwarePaginator Object
(
    [total:protected] => 1
    [lastPage:protected] => 1
    [items:protected] => Illuminate\Database\Eloquent\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => App\models\Job Object
                        (
                            [table:protected] => jobs
                            [guarded:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] =>
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] =>
                                    [client_id] => 26
                                    [category_id] => 2
                                    [title] => blah blah
                                    [location] => 2
                                    [date] => 2015-06-25
                                    [time] => 01:00:00
                                    [description] => blah bleh bluh
                                    [min_budget] => 2500
                                    [max_budget] => 3500
                                    [status] => 1
                                    [request_for] => 0
                                    [created_at] =>
                                    [updated_at] =>
                                    [message] =>
                                    [sender_id] =>
                                    [receiver_id] =>
                                    [job_id] =>
                                )

                            [original:protected] => Array
                                (
                                    [id] =>
                                    [client_id] => 26
                                    [category_id] => 2
                                    [title] => blah blah
                                    [location] => 2
                                    [date] => 2015-06-25
                                    [time] => 01:00:00
                                    [description] => blah bleh bluh
                                    [min_budget] => 2500
                                    [max_budget] => 3500
                                    [status] => 1
                                    [request_for] => 0
                                    [created_at] =>
                                    [updated_at] =>
                                    [message] =>
                                    [sender_id] =>
                                    [receiver_id] =>
                                    [job_id] =>
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [fillable:protected] => Array
                                (
                                )

                            [dates:protected] => Array
                                (
                                )

                            [casts:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [morphClass:protected] =>
                            [exists] => 1
                        )

                )

        )

    [perPage:protected] => 10
    [currentPage:protected] => 1
    [path:protected] => http://localhost:8000/vendors/jobs/
    [query:protected] => Array
        (
        )

    [fragment:protected] =>
    [pageName:protected] => page
)

为什么没有填充属性ID?

1 个答案:

答案 0 :(得分:1)

您应该自定义查询的SELECT部分。带有select *子句的join会返回两个id列,看起来Laravel会丢弃这两个列。

$jobs = Job::leftJoin('messages', function($join)
                             {
                               $join->on('jobs.id', '=', 'messages.job_id')
                                     ->where('messages.sender_id', '=', getLoggedInId());
                             })
       ->whereIn('category_id', $category_arr)
       ->where('status', 1)
       ->where('request_for', 0)
       ->groupBy('jobs.id')
       ->orderBy('messages.created_at', 'DESC')
       ->select(['jobs.*', 'messages.column1', 'messages.column2'])
       ->paginate(10);