【问题标题】:Searching data from API with Laravel PHP使用 Laravel PHP 从 API 中搜索数据
【发布时间】:2022-10-13 12:57:31
【问题描述】:

我尝试将搜索功能放在使用 API 显示数据库中表列表的页面上。但它总是出错,我不知道该尝试什么了。

这是我的源代码:

<?php

namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class PaymentDBController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    private $client; 

    public function __construct() {
        $this->client = new Client([
            'base_uri' => 'http://20.20.20.192:8585/api/v1/'
        ]);
    }
    public function index(Request $request) {

       $response = $this->client->request('GET', 'tables',[
            'query' => [
                'limit' => '100'
            ]
        ]);
        $data = json_decode($response->getBody()->getContents(), true)['data'];
        return view('cobaTable', compact('data'));

         if ($request->has('search')) {
        $take = Client::table->when($search, function ($query, $search) {
            return $query->where('name', 'LIKE', "%{$search}%");
        });
       } else {
        $take = Client::table::all();
       }
       }

这是错误之一:

调用未定义的方法 GuzzleHttp\Client::all()

我尝试了HttpGuzzle 方法,但仍然出现错误 请问有人可以给点建议吗?

【问题讨论】:

    标签: php laravel api http guzzle


    【解决方案1】:

    我也在使用 Guzzle。请使用这种类型的编码

    use GuzzleHttp;
    private $response;
    
    $client = new GuzzleHttpClient();
    $this->response= $client->request('GET', 'URL'); 
    

    【讨论】:

    • 谢谢你,先生。对于那种类型,我已经成功了。我试图弄清楚的这个是对那些 API 获取数据进行“搜索”||示例: $this->response= $client->request('GET', '127.0.0.1:table');我可以通过 Laravel-PHP 搜索请求来搜索这个表名列表
    猜你喜欢
    • 2021-12-12
    • 2019-09-26
    • 2017-05-29
    • 2018-10-24
    • 2020-04-04
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多