1

我在我的项目中使用 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
    )

)

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

4

2 回答 2

0

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

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

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

于 2015-09-19T14:00:56.567 回答
0

You are dealing with collections that are returned from the query. Don't think array merge is correct way to go Try using collection merge.

Collection merge is supported as well in laravel 4. Try using this

$merged_collection = $vehicles->merge($estates)->merge($tradings)->merge($jobs);
于 2015-09-19T14:14:17.180 回答