我正在尝试使用L5分页,直到我单击到第二页链接,一切看起来都很好。当我单击它时,出现此错误:
Laravel 5分页未定义可变车辆
控制者
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新手,我不知道该怎么办,如何解决此问题?任何帮助,将不胜感激。
您没有将$condition
分页追加,因此丢失了。在if
返回视图之前的语句中(在Controller中),仅在满足$vehicles
a时设置变量$condition
(没有回退),因此它不会传递$vehicles
给视图。如果实际上没有条件,则最好设置一个后备,将$vehicles
变量设置为某些值,否则会导致您遇到错误。
请注意,您还必须将其传递给视图!(您当前未这样做)
在您的视图中尝试以下操作:
{!! $vehicles->appends(['condition' => $condition])->render()!!}
并将其添加到您的控制器:
return view('pages.aracislemler', compact('vehicles', 'condition'...
来源:http : //laravel.com/docs/5.1/pagination(搜索追加)
编辑(如评论中所讨论,也正如@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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句