2016-05-17 50 views
0

我對Laravel和MVC結構相當陌生。 我想向頁面添加兩個按鈕,這將決定產品在其中的排列順序。 (價格 - 從低到高或從高到低)。 我試圖使用的查詢字符串,像這樣:如何在PHP上使用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 ::'來獲取'訂單'鍵)

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

如何將帶有ORDER BY元素的查詢添加到此模型中?如果密鑰出現,如何從查詢字符串中獲取值?

非常感謝!

回答

1

試試這個:

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; 
     } 
     ... 
    } 
} 
+0

謝謝,我已經試過了,但沒有任何反應。 有沒有可能這種方式$ order始終爲空? – user3185970

+0

有什麼想法? 因爲$ ordedr在發送給模型之前獲得了控制器中的值,所以模型可能會將其始終設置爲null? 謝謝! – user3185970

+0

您必須在調用getProducts時傳遞訂單:$ order = Input :: get('order'); Product :: getProducts($ cat_url,$ order,self :: $ data,$ order); – scrubmx

1

你說你想使用MVC結構,但你傳遞的順序網址像query parameter?馬克。這是不正確的方式在MVC你可以使用Routing這個嗎?

給一個嘗試這個..

註冊您的路線喜歡這個

Route::get('store/{cat_url}/{order?}', "[email protected]"); 

控制器方法

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> 

編碼快樂......

+0

謝謝。你能解釋什麼是產品論證中的「請求」部分? – user3185970

+0

'Request'由'Laravel'提供的類來獲取用戶當前請求的'Information'。它提供諸如請求方法(GET,POST ..),當前請求的URL和用戶提供的數據等信息。有關信息,可以閱讀它的文檔https://laravel.com/docs/5。2 /請求 –

+0

我想我寧願用這個?在查詢字符串:) 你認爲你的協助? 我仍然試圖把我的頭圍繞Laravel和MVC,所以你的解決方案對我來說太難了...... 無論如何感謝很多.. – user3185970