Laravel 5 分页 视图中未定义变量 vehicles

4

我想使用L5分页,一切看起来都很好,直到我点击第二页链接。当我点击它时,我遇到了这个错误:

Laravel 5 分页视图中的未定义变量“vehicles”

控制器:

public function search() {
    $vehicle = Vehicle::with('representive','seller','buyer','whoUpdated')->orderBy('created_at', 'desc')->first();;

    $previous = Vehicle::where('id', '<', $vehicle->id)->max('id');

    $next = Vehicle::where('id', '>', $vehicle->id)->min('id');

    $first = Vehicle::orderBy('created_at', 'asc')->first();
    $last = Vehicle::orderBy('created_at', 'desc')->first();


    //passing rapyd components
    $rapydProcess = new RapydController();
    $searchFilter = $rapydProcess->createSearchFilter();
    $searchGrid = $rapydProcess->createVehicleDataGrid($searchFilter);
    $vehicleTrackFilter = $rapydProcess->createVehicleTrackFilter();
    $vehicleTrackDataGrid = $rapydProcess->createVehicleTrackDataGrid($vehicleTrackFilter);
    //$statisticsDataGrid = $rapydProcess->statisticsDataGrid();

    //passing select box contents to view
    $clients = Client::lists('full_name', 'id');
    $vehicleTypes = VehicleType::lists('vehicle_type', 'id');
    $sections = Section::lists('section_name', 'id');
    $brands = Brand::lists('brand_name', 'id');
    $paymentTypes = PaymentType::lists('payment_type', 'id');
    $searchOptions = StatisticsSearchOptions::lists('option_name', 'slug');

    $option1 = Request::get('option1');
    $option2 = Request::get('option2');
    $condition = Request::get('condition');
    $date_option = Request::get('dateOption');
    $option1_value = Request::get('option1_value');
    $option2_value = Request::get('option2_value');
    $fromDate = Request::get('fromDate');
    $toDate = Request::get('toDate');

    if (isset($option1_value)) {
        $actual_option1 = '';
        foreach ($option1_value as $value) {
            if ($value != '') {
                $actual_option1 = $value;
            }
        }
    }

    if (isset($option1_value)) {
        $actual_option2 = '';
        foreach ($option2_value as $value) {
            if ($value != '') {
                $actual_option2 = $value;
            }
        }
    }

    if($condition == 'no'){
        $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->paginate(2);
        //return $vehicle;
    }
    if($condition == 'or'){
        $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->orWhere($option2, $actual_option2)->paginate(2);
    }
    if($condition == 'and'){
        $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->where($option2, $actual_option2)->paginate(2);
    }

    return view('pages.aracislemler', compact('vehicles','condition','option1','option2','actual_option1','actual_option2','vehicle','previous','next','first','last','searchFilter', 'searchGrid', 'vehicleTrackFilter', 'vehicleTrackDataGrid', 'statisticsDataGrid', 'vehicleTypes', 'sections', 'brands', 'clients','paymentTypes','searchOptions'));
    //return $vehicles;
}

查看

@if(isset($vehicles))
    @foreach($vehicles as $vehicle)
        <tr>
            <td style="padding: 15px;">{{ $vehicle->id }}</td>
            <td style="padding: 15px;">{{ $vehicle->brand_id }}</td>
            <td style="padding: 15px;">{{ $vehicle->model }}</td>
            <td style="padding: 15px;">{{ $vehicle->type_id }}</td>
            <td style="padding: 15px;">{{ $vehicle->licenseplate }}</td>
            <td style="padding: 15px;">{{ $vehicle->representive_client_id }}</td>
            <td style="padding: 15px;">{{ $vehicle->buyer_client_id }}</td>
            <td style="padding: 15px;">{{ $vehicle->seller_client_id }}</td>
            <td style="padding: 15px;">{{ $vehicle->debit_situation }}</td>
            <td style="padding: 15px;">{{ $vehicle->partner_situation }}</td>
            <td style="padding: 15px;"><a href="{{ URL::to( 'pages/vehicles?show=' . $vehicle -> id ) }}">Git</a></td>
        </tr>
    @endforeach
@endif

{!! $vehicles->appends(['condition' => 'condition','option1' => 'option1','option2' => 'option2','actual_option1' => 'actual_option1','actual_option2' => 'actual_option2'])->render()!!}

作为一个L5的新手,我不知道该怎么处理这个问题,如何解决这个问题? 感激不尽您的帮助。
2个回答

1
您没有将$condition附加到分页中,因此它会丢失。在控制器中返回视图之前的if语句中,仅在满足$condition时设置$vehicles变量(没有备用方案),因此它不会将$vehicles传递给视图。最好设置一个备用方案,以防实际上没有条件,将$vehicles变量设置为某些内容,否则会导致您遇到的错误。
请注意,您还必须将其传递到视图中!(您当前未执行此操作)
在视图中尝试类似以下的内容:
{!! $vehicles->appends(['condition' => $condition])->render()!!}

并将其添加到您的控制器中:

return view('pages.aracislemler', compact('vehicles', 'condition'...

来源: http://laravel.com/docs/5.1/pagination (搜索“appending”)

编辑(如评论中讨论的和@HieuLu所解释的:例如,您可能需要根据自己的需求更改最后一个else

if($condition == 'no'){
    $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->paginate(2);
    //return $vehicle;
}
elseif($condition == 'or'){
    $vehicles =             Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->orWhere($option2, $actual_option2)->paginate(2);
}
elseif($condition == 'and'){
    $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->where($option2, $actual_option2)->paginate(2);
} 
else {
    $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->paginate(2);
}

仍然存在相同的问题。 - Tartar
也许尝试将您需要的所有变量添加到附加中? - Francesco de Guytenaere
你现在是否遇到了错误?我不确定它在条件语句中使用的所有其他值(option1、option2等)是否为空。你可以使用var_dump()或dd()函数来调试vehicles变量。请问现在出了什么问题? - Francesco de Guytenaere
是的,我没有看到任何变化,这和我在更新后的答案中所写的差不多吗? :) - Francesco de Guytenaere
我在您的控制器中看不到更新后的代码,只能在您的视图中看到。但我想这并不重要,因为您正在追加它。 - Francesco de Guytenaere
显示剩余6条评论

0

当查询字符串中没有condition参数或该参数的值不是以下值之一:orandno时,您的代码中未设置$vehicles变量。当compact函数在变量未定义的情况下忽略一个变量而不是抛出错误时,会导致视图中出现错误。

引用自PHP文档

任何未设置的字符串都将被简单地跳过。

您需要稍微修改一下代码,选择一个默认值,以确保在将其发送到视图之前始终设置$vehicles

if($condition == 'no'){
    $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->paginate(2);
    //return $vehicle;
} elseif ($condition == 'or'){
    $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->orWhere($option2, $actual_option2)->paginate(2);
} else {
    # default value
    $vehicles = Vehicle::with('brand','section','representive','buyer','seller','buyingPaymentType','sellingPaymentType')->where($option1, $actual_option1)->where($option2, $actual_option2)->paginate(2);
}

默认值修复了分页问题,但在表单处理后它并不返回实际的搜索结果。 - Tartar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接