【问题标题】:Laravel 5 Pagination undefined variable vehicles in viewLaravel 5 Pagination 视图中未定义的变量车辆
【发布时间】:2015-09-18 19:17:48
【问题描述】:

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

Laravel 5 Pagination 视图中未定义的变量车辆

控制器

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新手,我不知道该怎么办,我该如何解决这个问题? 任何帮助将不胜感激。

【问题讨论】:

    标签: php laravel pagination laravel-5


    【解决方案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(搜索追加)

    编辑(如 cmets 中所述,@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);
    }
    

    【讨论】:

    • 还是有同样的问题
    • 也许尝试将您需要的所有变量添加到追加?
    • 除了条件我还需要什么?
    • 不确定,您现在遇到错误了吗?我不确定它在条件中使用的所有其他值(选项 1、选项 2 .. 等)现在是否为空。您能否使用 var_dump() 或 dd() 车辆进行调试。现在到底出了什么问题?
    • 我已经返回并附加了所有这些值,但仍然出现未定义的车辆错误
    【解决方案2】:

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

    引用PHP documetation

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

    您需要稍微修改您的代码,选择一个默认值以确保您的$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);
    }
    

    【讨论】:

    • 默认值修复了分页问题,​​但不返回表单处理后的实际搜索结果。
    猜你喜欢
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多