【问题标题】:Laravel/Lumen API print array of recordsLaravel/Lumen API 打印记录数组
【发布时间】:2018-08-25 21:31:24
【问题描述】:

是否有可能制作一个像这样打印数据库记录的 API:http://localhost:8000/products/?compare=1-2-N...(1,2,N) product id's. 我只成功打印了一条记录。我的路线:

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

和我的控制器:

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
        ]);

    }

你能帮我用这样的路线打印记录数组吗:

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

【问题讨论】:

  • 为什么您要尝试在 URL 中发送此数据并使其复杂化。我认为您可以在 POST 请求中发送这些并在控制器中处理它。
  • 看起来你应该能够使用 php explode 函数来创建一个 id 数组。然后你只需要循环它们

标签: php laravel routes blade lumen


【解决方案1】:

是的,这是可能的。您可以尝试通过 get 传递模式并对其进行解析以检索您需要的信息,但为什么不使用简单的方法来执行此操作,例如:

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

你想要的每件事都可以设计好,在你的控制器中,你有

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

【讨论】:

    【解决方案2】:

    我终于设法使用$tail = $request->tail;method 解决了这个问题,并在我的表单操作中添加了这样的请求 id

    @php
    $tail = basename(request()->path());
    $text = (string)$tail; 
    @endphp
    
    <form action="/product-compare/{{$text}}-{{ $product['id'] }}" method="post">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多