Livewire 句柄查询

您好,我正在尝试使用 livewire 创建搜索查询。我想得到这样的特定查询: http://127.0.0.1:8000/users?page=1&name=abc


我该如何处理我的疑问?现在我的控制器和刀片视图如下所示:


    public $page, $currentPage,$name;


    public function submit(Request $request)

    {

        $input = $request->all();

        dd($input);

      

    }

    

    public function mount(Request $request)

    {

        $value = session()->get('token');

        $page = $request->input('page');


        $client =new \GuzzleHttp\Client([

            'http_errors' => false,

            ]);

            

        $this->response = $client->get(env('API_URL').'users?', [

            'query' => [

                'page'=>$page,

                'name'=>$this->name,

            ],

             'headers' => [

                 'Authorization' => 'Bearer '.$value ,

                 'Accept' => 'application/json',

                ],

                ]);

                

        $this->res = json_decode($this->response->getBody(), true);

    }


HUWWW
浏览 78回答 1
1回答

慕容森

我假设您有一个带有输入功能的刀片,例如<input type="text" wire:model="name"/>按名称搜索。在挂载中,您可能希望传递请求中的数据,如下所示:public function mount(Request $request){&nbsp; &nbsp;...&nbsp; &nbsp; $this->name = request()->query('name');&nbsp; &nbsp;...}并删除挂载的责任并添加到如下函数中:private search($name){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $this->response = $client->get(env('API_URL').'users?', [&nbsp; &nbsp; &nbsp; &nbsp; 'query' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page'=> $page,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'=> $name,&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'headers' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Authorization' => 'Bearer '. $value ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Accept' => 'application/json',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $this->res = json_decode($this->response->getBody(), true);}然后添加一个函数来听取输入的更改:public function updatedName($name){&nbsp; &nbsp; $this->search($name);}页面查询我不确定你是从 API 还是查询字符串中获取它们。如果您使用模型,我建议您查看 WithPagination 特征。如果您使用 API 分页,则必须在前面手动应用页面的更改。
打开App,查看更多内容
随时随地看视频慕课网APP