在 php 上使用 MVC 结构时如何添加 'order by' 按钮?
How to add an 'order by' button when working with MVC structure on php?
我对 Laravel 和 MVC 结构还很陌生。
我想在一个页面上添加两个按钮,这将决定其中产品的显示顺序。 (价格 - 从低到高或从高到低)。
我正在尝试像这样使用查询字符串:
href="{{ url('store/' . $cat_url . '?order=ASC') }}">Price: Low to High</a> |
href="{{ url('store/' . $cat_url . '?order=DESC') }}">Price: Hign to Low</a>
这是我的产品视图:
@foreach($products as $row)
<div class="col-md-6">
<h3>{{$row['title']}}</h3>
<p><img border="0" width="300" src="{{asset('images/' .$row['image'])}}"></p>
<p>{!! $row['article'] !!}</p>
<p><b>Price for pack:</b> {{ $row['price'] }}</p>
<p>
<input @if(Cart::get($row['id'])) disabled="disabled" @endif data-id="{{ $row['id']}}" type="button" value="Add to Order" class="add-to-order btn btn-success">
<a href="{{ url('store/' . $cat_url . '/' . $row['url']) }}" class="btn btn-primary">More Details</a>
</p>
</div>
@endforeach
这是型号:
static public function getProducts($category_url, &$data)
{
$data['products'] = [];
if ($category = Category::where('url', $category_url)->first()) {
$category = $category->toArray();
$data['title'] = 'Candy | '. $category['title'];
$data['cat_url'] = $category['url'];
$data['cat_title'] = $category['title'];
$products = Category::find($category['id'])->products;
$data['products'] = $products->toArray();
}
}
他是控制器:(我正在尝试使用 'Input::' 获取 'order' 键)
public function products($category_url)
{
$order = Input::get('order');
Product::getProducts($category_url, self::$data);
return view('content.products', self::$data);
}
如何将带有 ORDER BY 元素的查询添加到此模型?如果出现键,如何从查询字符串中获取值?
非常感谢!
试试这个:
static public function getProducts($category_url, &$data, $order = null){
$data['products'] = [];
if($category = Category::where('url', $category_url)->first()){
...
if ($order) {
$products = Category::find($category['id'])->products()->orderBy('price', $order)->get()
} else {
$products = Category::find($category['id'])->products;
}
...
}
}
您说您想使用 MVC
结构,但您在 url 中传递了订单,例如 query parameter
和 ?
标记。这不是 MVC
中的正确方法,您可以使用 Routing
吗?
试一试这个..
按此注册您的路线
Route::get('store/{cat_url}/{order?}', "ControllerName@products");
控制器方法
use Illuminate\Http\Request;
public function products(Request request, $cat_url, $order="asc")
{
Product::getProducts($cat_url,$order, self::$data);
return view('content.products', self::$data);
}
模型方法
static public function getProducts($category_url, $order,&$data)
{
$data['products'] = [];
if ($category = Category::where('url', $category_url)->orderBy("your_field_name",$order)->first()) {
$category = $category->toArray();
$data['title'] = 'Candy | '. $category['title'];
$data['cat_url'] = $category['url'];
$data['cat_title'] = $category['title'];
$products = Category::find($category['id'])->products;
$data['products'] = $products->toArray();
}
}
您的链接
href="{{ url('store/' . $cat_url . '/asc') }}">Price: Low to High</a> |
href="{{ url('store/' . $cat_url . '/desc') }}">Price: Hign to Low</a>
快乐编码...
我对 Laravel 和 MVC 结构还很陌生。 我想在一个页面上添加两个按钮,这将决定其中产品的显示顺序。 (价格 - 从低到高或从高到低)。 我正在尝试像这样使用查询字符串:
href="{{ url('store/' . $cat_url . '?order=ASC') }}">Price: Low to High</a> | href="{{ url('store/' . $cat_url . '?order=DESC') }}">Price: Hign to Low</a>
这是我的产品视图:
@foreach($products as $row)
<div class="col-md-6">
<h3>{{$row['title']}}</h3>
<p><img border="0" width="300" src="{{asset('images/' .$row['image'])}}"></p>
<p>{!! $row['article'] !!}</p>
<p><b>Price for pack:</b> {{ $row['price'] }}</p>
<p>
<input @if(Cart::get($row['id'])) disabled="disabled" @endif data-id="{{ $row['id']}}" type="button" value="Add to Order" class="add-to-order btn btn-success">
<a href="{{ url('store/' . $cat_url . '/' . $row['url']) }}" class="btn btn-primary">More Details</a>
</p>
</div>
@endforeach
这是型号:
static public function getProducts($category_url, &$data)
{
$data['products'] = [];
if ($category = Category::where('url', $category_url)->first()) {
$category = $category->toArray();
$data['title'] = 'Candy | '. $category['title'];
$data['cat_url'] = $category['url'];
$data['cat_title'] = $category['title'];
$products = Category::find($category['id'])->products;
$data['products'] = $products->toArray();
}
}
他是控制器:(我正在尝试使用 'Input::' 获取 'order' 键)
public function products($category_url)
{
$order = Input::get('order');
Product::getProducts($category_url, self::$data);
return view('content.products', self::$data);
}
如何将带有 ORDER BY 元素的查询添加到此模型?如果出现键,如何从查询字符串中获取值?
非常感谢!
试试这个:
static public function getProducts($category_url, &$data, $order = null){
$data['products'] = [];
if($category = Category::where('url', $category_url)->first()){
...
if ($order) {
$products = Category::find($category['id'])->products()->orderBy('price', $order)->get()
} else {
$products = Category::find($category['id'])->products;
}
...
}
}
您说您想使用 MVC
结构,但您在 url 中传递了订单,例如 query parameter
和 ?
标记。这不是 MVC
中的正确方法,您可以使用 Routing
吗?
试一试这个..
按此注册您的路线
Route::get('store/{cat_url}/{order?}', "ControllerName@products");
控制器方法
use Illuminate\Http\Request;
public function products(Request request, $cat_url, $order="asc")
{
Product::getProducts($cat_url,$order, self::$data);
return view('content.products', self::$data);
}
模型方法
static public function getProducts($category_url, $order,&$data)
{
$data['products'] = [];
if ($category = Category::where('url', $category_url)->orderBy("your_field_name",$order)->first()) {
$category = $category->toArray();
$data['title'] = 'Candy | '. $category['title'];
$data['cat_url'] = $category['url'];
$data['cat_title'] = $category['title'];
$products = Category::find($category['id'])->products;
$data['products'] = $products->toArray();
}
}
您的链接
href="{{ url('store/' . $cat_url . '/asc') }}">Price: Low to High</a> |
href="{{ url('store/' . $cat_url . '/desc') }}">Price: Hign to Low</a>
快乐编码...