如何将一个类别重定向到子类别?

时间:2019-01-23 11:50:05

标签: laravel laravel-5 eloquent laravel-5.2 laravel-5.1

这是FrontController

public function index(){
    $categories = DB::table('categories')
        ->select('category')
        ->groupBy('category')
        ->get();
    return view('front', compact('categories'));
}

这是刀片的布局

@foreach($categories as $category)
<a href=""><h2 class="card-title">{{$category->category}}</br></h2></a>
@endforeach

Categories-table structure and data

This is my front page

我的问题是,当我单击任何重定向到与子类别相关的类别时,该怎么做?

2 个答案:

答案 0 :(得分:2)

在您的控制器中,您可以执行以下操作:

public function curlrequest(){
    $data = array( 
        "page_id" => $this->page_id,                        
        "Currency" => get_woocommerce_currency(),
        "amount" => '100',
        "datetime_utc" => date('Y-m-d H:i:s'),
        "transaction_type" => "authorize",
        "billing_address"=>('first_name'=>'','lastname'=>'') // need to pass here
    );                                                                    
    $data_string = json_encode($data);                                                                                  

    $ch = curl_init('https://xxxxxxxxxx/paymentgateway');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',  
        'Authorization: Basic '. base64_encode("$this->api_username".':'."$this->api_password"),                                                                              
        'Content-Length: ' . strlen($data_string))                                                                       
    );  

    return respone token;                 
}

public function payment_fields() {
    echo '<input id="checkout_token" name="checkout_token" type="hidden" value="'.$this->curlrequest().'" />';
    //other cc common fields
}

在刀片中:

// for categories
public function index() {
    $categories = DB::table('categories')
        ->select('category')
        ->groupBy('category')
        ->get();
    return view('front', compact('categories'));
}

// for sub categories
public function subCategory($category) {
    $sub_categories = DB::table('categories')
        ->where('category', $category)
        ->get();
    return view('another_front', compact('sub_categories'));
}

路线为:

// for category
@foreach($categories as $category)
    <a href="{{ url('/subcategory') }}/{{$category->category}}"><h2 class="card-title">{{$category->category}}</br></h2></a>
@endforeach

// for sub category
@foreach($sub_categories as $subcategory)
        <a href="#"><h2 class="card-title">{{$subcategory->sub_category}}</br></h2></a>
    @endforeach

答案 1 :(得分:0)

您可以尝试

这是路由文件“ web.php”

project_url /category/{catid}

这是刀片的布局

@foreach($categories as $category) <a href="{{project url / category/$category->id}}"><h2 class="card-title">{{$category->category}}</br></h2></a> @endforeach