Laravel 9-从一个控制器传递每个视图的数据

发布于 2025-02-11 23:57:44 字数 1449 浏览 0 评论 0原文

你好,我是新来的拉拉维尔。我的应用程序几乎在每个视图中都有一个搜索栏组件。 用户键入客户端的ID,对DB进行查询,并将用户的键入ID与DB客户端ID进行比较。

控制器:

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class SearchController extends Controller
 {
   function get_kcli(Request $request) {
    
    $id = $request->input('kcli');
    $current_page = $request->input('currentPage') . '.index';
    $data = DB::connection('oracle')->table('CLIENTS')->where('KCLI', $id)->get();

   return view($current_page, compact('data'));       
  }
}

Web.php

Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');

表单搜索组件视图:

<nav class="navbar navbar-light">
<form method="POST" class="form-inline position-relative" action="{{ route('/search') }}">
    @csrf
    @method('POST')
  <input class="form-control shadow-none" name="kcli" id="kcli" type="number" placeholder="Codice..." aria-label="Search">
  <input type="text" name="currentPage" value="{{ Route::currentRouteName() }}" hidden>
  <button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>

任何人都知道在需要挖掘出来的每个视图中存储该数据请求的最佳实践吗?

示例:我搜索客户端ID'5',搜索控制器与传递ID进行了DB查询,然后压实数据。该数据必须在全球存储以获取结果并将其发布在其他视图中,而无需在每个视图开关上再次搜索(键入的ID'5'必须保留在每个视图开关的搜索字段中)。

Hello i'm new in laravel. My application has a search bar component in almost every view.
The user types there the ID of the client, makes a query to db and compares the user's typed ID with the DB client ID.

Controller:

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class SearchController extends Controller
 {
   function get_kcli(Request $request) {
    
    $id = $request->input('kcli');
    $current_page = $request->input('currentPage') . '.index';
    $data = DB::connection('oracle')->table('CLIENTS')->where('KCLI', $id)->get();

   return view($current_page, compact('data'));       
  }
}

Web.php

Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');

Form search component view:

<nav class="navbar navbar-light">
<form method="POST" class="form-inline position-relative" action="{{ route('/search') }}">
    @csrf
    @method('POST')
  <input class="form-control shadow-none" name="kcli" id="kcli" type="number" placeholder="Codice..." aria-label="Search">
  <input type="text" name="currentPage" value="{{ Route::currentRouteName() }}" hidden>
  <button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>

Anyone knows the best practice to store that data request globally an mantain it for every view that need to exctract it?

Example : I search for the client ID '5', the search controller makes a db query with the passed ID, then compacts data. That data has to be stored globally for get the results and post it in other views, without searching again on every view switch(the typed ID '5' has to remain in the search field on every view switch).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2025-02-18 23:57:44

要打印client_id,您可以在会话中存储该“ ID”,然后以这种方式从会话中打印您的搜索值保留​​保留并在所有视图中获取数据概念视图::您可以在AppServiceProvider中注册,请浏览此信息链接

https://laravel.com/laravel.com/docs/docs/9 .x/views#sharing-data-with-all-views

To print client_id you can store that "id" in session and print from the session by this way your search value keep remains and to get data in all views there is the concept View::share you can register that in AppServiceProvider please go through this link

https://laravel.com/docs/9.x/views#sharing-data-with-all-views

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文