Laravel Query结果在创建集合后被删除&分页错误?

时间:2014-12-03 10:07:04

标签: php database laravel collections eloquent

此查询有效并返回我想要的结果但是一旦转为集合或尝试“分页”它将返回空结果。我尝试对一个简单的DB::table('users')->paginate(5)[0]进行分页并且它有效,所以它不是来自我想的分页。

代码

$photographers_available =  Photographer::where('is_photographer', '=', '1')
          ->where(function($q)use($city_id)
              {
            $q->whereHas('studioAddress', function($q)use($city_id)
         {
              $q->where('city_id', '=', $city_id);
         })
         ->orWhereHas('user', function($q)use($city_id)  // TK could reduce to 'user.address' I think
             { 
                $q->whereHas('address', function($q)use($city_id) 
              {
                 $q->where('city_id', '=', $city_id);
              });
             });
        })
          ->whereHas('stypesPhotographer', function($q)use($stype)
          {
        $q->where('shooting_type_id', '=', $stype);
          }) 
          ->whereHas('availabilities', function ($q) use ($selected_date_time)
              {
        $q->where('unavailable_start_date', '<', $selected_date_time)
          ->where('unavailable_end_date', '>', $selected_date_time);
        }, '=', 0)
          ->whereHas('bookings', function($q)use($selected_date_time)
              {
            $q->where('booking_date', '=', $selected_date_time);
        }, '=', 0)
          ->join('stypes_4_photographer', function($join)use($stype)
          {
            $join->on('photographer_id', '=', 'photographers.id')
               ->where('shooting_type_id', '=', $stype);
          });
 $photobla = $photographers_available->first();
 echo '<pre>';print_r($photobla);
 // RETURNS RESULTS CORRECTLY
 $photoTEST = $photographers_available->get();
 echo '<pre>';print_r($photoTEST);die();
 // RETURNS $photoTEST as NULL.....
 // WHY has $photographers_available BEEN EMPTIED?????

结果

// $photobla result
Photographer Object
(
    [fillable:protected] => Array
        (
            [0] => id
            [1] => is_photographer
            [2] => service_description
            [3] => radius_around_city_available
            [4] => tax_nbr
            [5] => user_id
            [6] => average_score
        )

    [table:protected] => photographers
    [touches:protected] => Array
        (
            [0] => stypesPhotographer
        )

    [connection:protected] => 
    [primaryKey:protected] => id
    [perPage:protected] => 15
    [incrementing] => 1
    [timestamps] => 1
    [attributes:protected] => Array
        (
            [id] => 677
            [is_photographer] => 1
[ETC>>>>>>>>]
         )
    [original:protected] => Array
        (
            [id] => 677
            [is_photographer] => 1
[ETC>>>>>>>>]
 )

    [relations:protected] => Array
        (
        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

    [dates:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

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

// THE $photoTEST result:
Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
        )

)

所以你可以看到$photographers_available->first()之后被清空了。除非在初始查询中直接使用,否则在该paginate之上将无法使用它。

1 个答案:

答案 0 :(得分:0)

$photographers_available; // it is a query, consider other name

$photographers_available->first(); 
// set limit 1 on the query and return it's result

$photographers_available->get(); 
// returns the query result (remember limit 1 above..)

$photographers_available->paginate(); 
// return the query result paginated (ignoring limit 1 above)

我建议你这样做(没有first电话):

dd($photographers_available->get());

return这件事,而不是echo whataver,因为这不是方法。在控制器中输出这样的内容会破坏应用流程。


回答您的问题,例如在评论中:将laravel / framework恢复为之前版本(在本地/ dev服务器中)