【问题标题】:get the Id from a item grid and save into a variable从项目网格中获取 Id 并保存到变量中
【发布时间】:2019-04-19 00:22:42
【问题描述】:

我正在使用 Laravel 5 执行以下操作:我有一个显示公司拥有的所有客户的网格,在该网格的每一行中,都有一个按钮可以选择该客户,如下所示:

Grid of clients

此按钮将我转到与所选客户端直接相关的单独屏幕,发生这种情况时,URL 如下:

http://sistemaprueba.com/modulos?1

其中问号后面的值代表所选客户端的id值。现在,我需要将该 id 的值保存在一个变量中以便以后使用,我该怎么做?

这是我的网格:

<div class="row">
    <div class="table-responsive">
        <table class="table">
            <thead>
            <tr>
                <th>Nombre Cliente</th>
                <th>Ingresar</th>
            </tr>
            </thead>
            <tbody>
                @foreach($results as $result)
                    <tr>
                        <td>{{$result->nombre_cliente}}</td>
                        <td><a href="{{ route ('modulos',[$result->id])}}" class="btn-box-tool" ><i class="fas fa-sign-in-alt" aria-hidden="true"></i></a>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>

这是通往新屏幕的路线:

Route::get('/modulos', 'HomeController@modulos')->name('modulos');

我的控制器看起来像这样:

class ClientController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $id = \Auth::user()->id;

        $results = DB::table('clients')->join('clients_company', 'clients_company.fk_id_client', '=', 'client.id')
            ->where('clients_company.fk_id_usuar',$id)->get();

        return view('clients',compact('results'));
    }

    public function modulos()
    {
        return view('modulos');
    }
}

【问题讨论】:

    标签: laravel laravel-5 routes laravel-routing


    【解决方案1】:

    您可以将您的 href 更改为“/modulos/1”,并将您的路由和控制器更改为:

    // route
    Route::get('/modulos/{clientId}', 'HomeController@modulos')->name('modulos');
    
    // controller
    public function index($clientId)
    {
      // Use $clientId
    }
    

    或者在你的url中给出一个命名参数,比如“/modulos?clientId=1”,那么:

    // route (unchanged)
    Route::get('/modulos', 'HomeController@modulos')->name('modulos');
    
    // controller
    public function index()
    {
      $clientId = request()->input('clientId');
      ...
    }
    

    或者,您可以在控制器中使用 PHP 的函数 parse_url() 解析 url,以提取 url 的 GET 部分供您操作。不是最干净的解决方案。 http://php.net/manual/en/function.parse-url.php

    第一种方法可能更适合您的目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 2021-05-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      相关资源
      最近更新 更多