如何添加';订单依据';按钮,当在php上使用MVC结构时


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();
    }
}

他的是控制器:(我正试图使用"输入::"获得"订单"键)

public function products($category_url)
{
    $order = Input::get('order');
    Product::getProducts($category_url, self::$data);
    return view('content.products', self::$data);
}

如何将带有ORDERBY元素的查询添加到此模型中?如果键出现,如何从查询字符串中获取值?

非常感谢!

试试这个:

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>

快乐编码。。。

相关文章: