【问题标题】:Passing Parameters from one view to another Laravel将参数从一个视图传递到另一个 Laravel
【发布时间】:2017-11-17 02:15:58
【问题描述】:

我是 Laravel 的新手,有些概念我不太了解。我真的需要你的帮助,因为我已经阅读了很多页面、教程和 stackoverflow 解决方案,但我没有得到我需要的结果。 我在数据库的索引页面上显示了一个“患者”列表。 代码如下:

 @if($data)
    <table class="table">
    <thead>
    <tr>
        <td>Nombre de Paciente</td>
        <td>Cedula</td>
        <td>Fecha</td>
        <td>Telefono</td>
        <td>Email</td>
        <td>Direccion</td>
        <td>Fecha de Creación</td>
        <td></td>
        </tr>
        </thead>
        <tbody>
        @foreach($data as $row)
        <tr>
        <td>
        <a href="{{url('/care/$row->id')}}"> {{ $row->paciente }}</a></td>
            <!--<td>{{ $row->paciente }}</td>-->
            <td>{{ $row->cedula }}</td>
            <td>{{ $row->fecha_nacimiento }}</td>
             <td>{{ $row->telefono }}</td>
            <td>{{ $row->email }}</td>
            <td>{{ $row->direccion }}</td>
            <td>{{ $row->created_at }}</td>
            <td>

            </td>
        </tr>
        </tbody>
        @endforeach
    </table>
    @endif

我想要做的,正如你所看到的,“患者”名称是一个链接,它指的是(在这种情况下)一个表格。 我想从中得到两件事。 一:我想获取我点击的患者的 ID 和“患者”的姓名,并将其发送到表单页面。 二:在表格页面上,我想在表格顶部显示“患者”姓名,并隐藏“患者ID”以将其保存在表格表中(此患者ID将是一个外键)

这是我当前的组件:

路线:

Route::get('/care/{id}', [
'as' => 'create',
'uses' => 'CareController@create' ]);

控制器

 public function create($id)
{
    //

    return view ($this->path.'.care',['id'=>$id]);
}

查看:

    <div class="form-group">

    <label for="exampleInputEmail">Paciente </label>
  <input type="hidden" name="id" value="{{$id}}">

  </div>

但是每当我加载页面时,它会显示一个空白页面,并且在 url 上我看到以下内容:

http://hospital.dev/care/$row->id

非常感谢您对此提供的帮助。

更新: 谢谢。 它现在发送ID。但是当试图显示表单页面时,它仍然发送空白页面,并且 url 如下: "http://hospital.dev/care/1"

不知道是不是控制器上的东西:

控制器: 公共函数创建($id) { //

    return view ($this->path.'.care',['id'=>$id ]);
}

我应该如何将 id 值传递给 url 而不是视图?

【问题讨论】:

  • 谢谢大家。它已经开始工作了。 :)

标签: php laravel controller routes parameter-passing


【解决方案1】:

您可以简单地连接 href:

<a href='/care/{{$row-id}}'></a>

一旦您将 id 传递到您的路线中。然后,您可以处理路由定义并将其传递给您的其他控制器。

例如创建动态链接的视图:

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Endpoint</div>
                </div>

                <table class="table table-bordered" id="endpoint-table">
                    <thead>
                    <tr>
                        <th>id</th>
                        <th>name</th>
                        <th>ip</th>
                        <th>mac</th>
                        <th>proxy</th>
                        <th>model</th>
                    </tr>
                    </thead>
                    <tbody id="endpoint-table-body">
                        @foreach($endpoints as $endpoint)

                            <tr><td><a href="/endpoint/{{$endpoint->id}}">{{$endpoint->id}}</a></td><td>{{$endpoint->name}}</td><td>{{$endpoint->ip}}</td><td>{{$endpoint->mac}}</td><td><a href="/proxy/{{$endpoint->proxy}}">{{$endpoint->proxy}}</a></td><td><a href="/model/{{$endpoint->model}}">{{$endpoint->model}}</a></td></tr>

                        @endforeach
                    </tbody>
                </table>


            </div>
        </div>
    </div>

@endsection

然后在你的 routes/web.php 中:

Route::get('/endpoint/{id}', 'EndpointController@show');

然后它被传递给控制器​​@函数显示你可以在哪里做一些逻辑:

/**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $endpoint = Endpoint::getObjectById($id);

        $e = new \stdClass();

        $e->id = $endpoint->id;
        $e->name = $endpoint->name;
        $e->ip = $endpoint->ipaddress;
        $e->mac = $endpoint->macaddress;
        $e->model = $endpoint->model_id;
        $e->proxy = $endpoint->proxy_id;

        $endpoint = $e;

        $endpoints = array();

        $endpoints[] = $endpoint;

        return view('endpoints', ['endpoints' => $endpoints]);

    }

当您返回视图时。您可以传递从您在控制器上定义的函数传递的数据。在这种情况下,该函数是 EndpointController 上的 show()。

【讨论】:

    【解决方案2】:

    url是http://hospital.dev/care/$row-&gt;id的原因是因为你用的是单引号,所以取不到变量的值。

    在列表中替换:
    {{url('/care/$row-&gt;id')}}
    与:
    {{url('/care/'+$row-&gt;id)}}

    【讨论】:

    • 你的意思是双引号而不是+?
    【解决方案3】:

    在这里,您将变量 $row-&gt;id 回显为字符串 &lt;a href="{{url('/care/$row-&gt;id')}}"&gt;,您需要打印该值。

    既然你已经命名了路线,我建议你使用route() helper 像这样重写它:

    <a href="{{ route('create', ['id' => $row->id]) }}">
    

    在这里,我调用了 route() 助手,将路由名称 create 作为第一个参数传递,并将路由期望的 {id} 作为第二个参数传递。

    另一种方法是更正您的方法,将变量移出'' 并将其附加到 url,如下所示:

    <a href="{{url('/care/' . $row->id)}}">
    

    Period . 将一个字符串附加到 php 中的另一个字符串,而不是 +

    【讨论】:

    • 它正在发送 id。但是当尝试显示表单页面时,它仍然发送空白页面,并且url如下:“hospital.dev/care/1”
    • 谢谢。它现在发送ID。但是当试图显示表单页面时,它仍然发送空白页面,并且url如下:“hospital.dev/care/1”我不知道它是否是控制器上的东西:CONTROLLER:public function create($id){/ / 返回视图 ($this->path.'.care',['id'=>$id ]);我应该如何将 id 值传递给 url 而不是视图?
    • 现在您必须将逻辑写入控制器的create($id) 方法中。您有患者$id,您需要查询您的数据库中的患者,将其传递给视图并在视图中执行您的表单操作。
    【解决方案4】:

    您使用的链接类似于 id')}}"> 所以在这里您将 id 打印为字符串。但是你需要像这样使用它

    id)}}">

    或

    id}}">

    【讨论】:

      猜你喜欢
      • 2015-10-23
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      相关资源
      最近更新 更多