2017-06-14 140 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}}「>

相關問題