2017-06-14 80 views
0

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

@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 

我想做什么,正如你所看到的,“患者”的名字是一个链接,这referese到(在这种情况下)的形式。 我想从中得到两件事。 ONE:我想获取我点击的患者的ID和“患者”的名称并将其发送到表单页面。 TWO:在窗体页面上,我想在窗体顶部显示“患者”名称,并隐藏“patient_id”以将其保存在表格上(此patient_id将是一个正在使用的密钥)

这是我目前的组件:

ROUTE:

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

控制器

public function create($id) 
{ 
    // 

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

VIEW:

<div class="form-group"> 

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

    </div> 

但每当我加载页面时,它显示了一个空白页面,并在网址我看到以下内容:

http://hospital.dev/care/ $按行> ID

我真的很感激这个你的帮助。

更新: 谢谢。 它现在发送的ID。但是,试图显示表单页面时,它仍然发送空白页,网址是: “http://hospital.dev/care/1

我不知道这是否是在控制器上的东西:

控制器: 公众函数来创建($ ID) {//

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

应该怎么id值没有传递到url,但到视图?

+0

谢谢大家。它的工作了。:) – Pisicato

回答

0

你可以简单地串联在href:

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

一旦通过将ID为您的口腔溃疡TE。然后您可以处理路由定义并将其传递给其他控制器。

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

@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 

那么在你的路由/ web.php:

Route::get('/endpoint/{id}', '[email protected]'); 

那么它被传递给控制器​​@功能显示在那里你可以做一些逻辑:

/** 
    * 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()。

0

url为http://hospital.dev/care/$row->id的原因是因为您使用的是单引号,所以它不会得到变量的值。

在替换列表:
{{url('/care/$row->id')}}
有了:
{{url('/care/'+$row->id)}}

+0

你的意思是双引号代替+? –

+0

@DovBenyominSohacheski是啊,这也将工作 –

0

在这里,我们呼应变量$row->id为字符串<a href="{{url('/care/$row->id')}}">,您需要打印的价值。

既然你已经得到了你的路线命名,我建议你把它改写这样的使用route()帮手:

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

在这里,我叫route()帮手,所经过的路由名称create作为第一个参数和{id}的路线期望作为第二个参数。

另一种方法是只纠正你的方法,通过移动可变出''并将其附加到这样的网址:

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

.将一个字符串追加到PHP的另一个字符串,而不是+

+0

其发送的ID,但是当试图显示放置表单页面,它仍然发送空白页面,URL为以下内容: “http://hospital.dev/care/1” – Pisicato

+0

谢谢。它现在发送的ID。但是当试图显示表单页面时,它仍然发送空白页面,URL如下:“http://hospital.dev/care/1” 我不知道它是否在控制器上: CONTROLLER:public function create($ id){// return view($ this-> path。'care',['id'=> $ id]); } 我应该如何传递id值no到网址,但视图? – Pisicato

+0

那么现在你必须将你的逻辑写入你的控制器的'create($ id)'方法中。你得到了患者'$ id',你需要为病人查询你的数据库,将它传递给视图,并在视图中做你的表单。 – devk

0

您所使用的链接就像是身份证')}}‘> 所以在这里要打印的ID作为字符串,但你需要使用这个像

ID)}}’>

OR

ID}}“>

相关问题