【问题标题】:Laravel Pagination mergeLaravel 分页合并
【发布时间】:2015-12-16 14:25:36
【问题描述】:

我在我的项目中使用 laravel 4。 我对 4 个表的选择结果进行了分页,我想将它们传递到一个变量中查看。

$vehicles = DB::table('vehicle')->orderBy('postTime','DESC')->paginate(8);
$estates = DB::table('estate')->orderBy('postTime','DESC')->paginate(8);
$tradings = DB::table('trading')->orderBy('postTime','DESC')->paginate(8);
$jobs = DB::table('job')->orderBy('postTime','DESC')->paginate(8);

但我无法使用 array_merge() 合并它们。分页后我也试过->to_array(),还是不行。

有人可以帮我吗?

编辑

正如@Turgut Sarıçam 建议的那样,我使用getItems() 作为我的结果,这是我的代码:

public static function index(){
    $minVehicle = DB::table('vehicle')->min('price');
    $maxVehicle = DB::table('vehicle')->max('price');
    $minEstate = DB::table('estate')->min('price');
    $maxEstate = DB::table('estate')->max('price');
    $minTrading = DB::table('trading')->min('price');
    $maxTrading = DB::table('trading')->max('price');
    if($minVehicle == 'توافقی')
        $minVehicle = 0;
    if($maxVehicle == 'توافقی')
        $maxVehicle = 0;
    if($minEstate == 'توافقی')
        $minEstate = 0;
    if($maxEstate == 'توافقی')
        $maxEstate = 0;
    if($minTrading == 'توافقی')
        $minTrading = 0;
    if($maxTrading == 'توافقی')
        $maxTrading = 0;
    $vehicles = DB::table('vehicle')->orderBy('postTime','DESC')->paginate(8);
    $estates = DB::table('estate')->orderBy('postTime','DESC')->paginate(8);
    $tradings = DB::table('trading')->orderBy('postTime','DESC')->paginate(8);
    $jobs = DB::table('job')->orderBy('postTime','DESC')->paginate(8);
    $allResults = array_merge($vehicles,$jobs);
    //echo "<meta charset='UTF8'><pre>";
    //return die(print_r($allResults));
    return View::make('search')->with(
        array(
            'minVehicle' => $minVehicle,
            'maxVehicle' => $maxVehicle,
            'minEstate' => $minEstate,
            'maxEstate' => $maxEstate,
            'minTrading' => $minTrading,
            'maxTrading' => $maxTrading,
            'ads' => $allResults
        )
    );
}

转储 $allResults(我评论过的地方)有一些结果。 我无法显示确切的结果,但这里是它的示例:

Array
(
[0] => stdClass Object
    (
        [id] => 243
        [tableName] => vehicle
    )

[1] => stdClass Object
    (
        [id] => 239
        [tableName] => vehicle
    )

[2] => stdClass Object
    (
        [id] => 235
        [tableName] => vehicle
    )

[3] => stdClass Object
    (
        [id] => 231
        [tableName] => vehicle
    )

[4] => stdClass Object
    (
        [id] => 227
        [tableName] => vehicle
    )

[5] => stdClass Object
    (
        [id] => 223
        [tableName] => vehicle
    )

[6] => stdClass Object
    (
        [id] => 219
        [tableName] => vehicle
    )

[7] => stdClass Object
    (
        [id] => 215
        [tableName] => vehicle
    )

[8] => stdClass Object
    (
        [id] => 4
        [tableName] => job
    )

[9] => stdClass Object
    (
        [id] => 3
        [tableName] => job
    )

)

虽然结果似乎是公平的,但没有返回任何视图。只是一张白纸

【问题讨论】:

  • 你能复制你的代码而不是截图吗?
  • 当然。看看编辑2
  • die(print_r($allResults)) 的结果是什么
  • @MwaaJoseph 添加到问题正文中;
  • 然后尝试传递 $allResults->toArray();

标签: php laravel laravel-4 pagination laravel-pagination


【解决方案1】:

你应该先拿到物品。试试这样:

$allResults = array_merge($vehicles->items(), $estates->items(), $tradings->items(), $jobs->items());

编辑:我猜你可以在 Laravel 4 中使用getItems() 函数获取项目。以上是 Laravel 5,我的错。

【讨论】:

  • 好吧,我刚刚在 Laravel 5 中尝试过,它可以工作。对查询进行分页时,返回值不是数组。它是一个分页器实例。您需要数组才能通过 array_merge 合并它们。
  • 嗯,使用 getItems() 之后,laravel 不会生成视图。它只是一个空白的白页,没有错误。我尝试传递 $allResults 甚至一个结果,例如 $vehicles->items() 并且发生了这种情况。
  • 您可以将变量传递给这样的视图:return view('your_view_file')-&gt;with('allResults', $allResults),然后像这样从您的视图中获取结果@foreach($allResults as $result) {{ var_dump($result) }} @endforeach
  • 我知道那部分。我在-&gt;with() 中使用了关联数组,它在其他传递的变量上运行良好。但是当我通过 $allResults 时,我得到空白页。
  • 您尝试转储 $allResults 了吗?真的是空的吗?
【解决方案2】:

您正在处理从查询返回的集合。不要认为数组合并是正确的方法尝试使用集合合并。

laravel 4 也支持集合合并。 试试这个

$merged_collection = $vehicles->merge($estates)->merge($tradings)->merge($jobs);

【讨论】:

  • 我正在使用 laravel 4.2 。我看不懂代码
  • 变量merged_collection 将包含所有其他集合。您可以在此处查看收集方法的完整列表laravel.com/api/4.2/Illuminate/Database/Eloquent/…
  • 我实际上从未使用过 Collection 。你能举个例子,它在我的情况下是如何工作的吗?
  • 那也没用。我在尝试 Turgut Sarıçam 回答时遇到了同样的问题。它返回空白页。另外,在我的 IDM 建议中,merge 并不是真正的方法,它被标记了;
猜你喜欢
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
  • 2015-06-06
相关资源
最近更新 更多