简体   繁体   中英

Laravel/Lumen API print array of records

Is it possible to make an API which prints database records like this: http://localhost:8000/products/?compare=1-2-N...(1,2,N) product id's. I have succeeded printing only one record. My route:

$router->get('products/{id}','ProductController@getProduct');

and my controller:

public function getProduct($id){

        $tlt_products = DB::table('tlt_products')->find($id);
        $tlt_products_features_id = DB::table('tlt_product_features')->where('product_id', $id)->get()->pluck('feature_id');
        $tlt_features = DB::table('tlt_features')->whereIn('id', $tlt_products_features_id)->get()->groupBy('feature_group');
        $tlt_feature_groups =  DB::table('tlt_features')->groupBy('feature_group')->get()->toArray();

        return response()->json([
            'product' => $tlt_products,
            'product_features' => $tlt_features,
            'feature_groups' => $tlt_feature_groups
        ]);

    }

could you please help me printing array of records using route like this:

http://localhost:8000/products/?compare=1-2-3...-N

Yes, it can be possible. You can try to pass a pattern through get and parse it to retrieve the information your need, but why don't you use a simple way to do that such as:

$router->get('products/{begin}/{end}','ProductController@getProduct');

every thing you want it possible just design it well and in your controller, you have

public function getProduct($begin,$end){
...
}

I finally managed to solve this issue using $tail = $request->tail; method and adding requested id like this on my form action

@php
$tail = basename(request()->path());
$text = (string)$tail; 
@endphp

<form action="/product-compare/{{$text}}-{{ $product['id'] }}" method="post">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM