在我正在处理的网站中,我在渲染视图时遇到了一些问题。在网站产品列表页面上,有一个小“排序方式...”元素。当用户选择其中一个选项时,它应该重新加载带有排序产品的产品页面,或者在不同的 URL 上加载带有排序产品的相同视图。任何一个都可以,我只想显示订购的产品。对我的产品进行排序ProductsController
工作得很好,但是当我想将这些排序的产品传递给前端时,我一直遇到问题。我在下面的代码中返回的视图与产品列表页面的普通视图相同。仅供参考:我对 Laravel 比较陌生,以前主要使用 JavaScript,所以我可能会尝试以一种非常愚蠢的非 Laravel 方式来做这件事。
理想情况下,我只想将排序后的产品传递给产品列表页面的 Blade 文件。我试过了,但我不知道如何触发重新加载。然后我尝试将用户引导到一条新路线(我在 web.php 中注册),我计划在该路线上呈现相同的产品视图,但数据已排序。这也不起作用,并给了我一个带有模糊 jQuery 错误消息的空白页面。
在 ProductsController.php 中:
public function sortController($type) {
$available_products = Products::with(['gallery', 'slug'])->get()
->where('status', 'available');
$number_of_products = count($available_products);
$product_names_sorted_by_type
= $this->orderProductTitlesBasedOnNumber($number_of_products, $available_products, $type);
$sorted_products_array = $this->orderProductsBasedOnSortedArray($number_of_products, $available_products, $product_names_sorted_by_type);
$product_brands_controller = new ProductsBrandsController;
$brands_list = $product_brands_controller->getBrandNamesArray();
return view('aanbod')->with([
'title' => 'Ons aanbod',
'products' => $sorted_products_array,
'brands' => $brands_list
]);
}
在我的 App.js 中:
function handleSortRequest(sortRequest) {
sortURL = window.location.origin + '/autos/list/sort?q=' + sortRequest
location.href = sortURL
}
在我的 Web.php 中:
Route::group(['prefix' => config('site.products.url'), 'as' => 'products.'], function () {
// some other routes...
Route::get('/list/sort/{sort_request}', 'ProductsController@handleSortRequest')->name('sort');
});
无论如何,这不起作用,根本没有渲染。我只是得到一个带有以下错误的空白页:“TypeError: a is undefined - javascript:2771” 这个错误似乎发生在 PHPDebugbar 使用的 jQuery 文件中。
希望这不是太多的文字。在此先感谢,让我知道如何改进我的 Laravel 代码!