2017-02-27 57 views
0

嘿,朋友我正在創建一個簡單的模式來向我顯示提供者的數據,並且老實說我花了很多錢;有人能幫我一把嗎?Modal with Laravel中的控制器

模態:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" 
    aria-labelledby="myModal"> 
    <div class="modal-dialog" 
     role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span 
          aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title" id="myModal">Detalle del Proveedor: </h4> 
      </div> 
      <div class="modal-body"> 

       <div class="table-responsive"> 
        <table class="table table-stripped table-bordered table-hover" id="table-detalle-proveedores"> 
         <thead> 
         <tr> 
          <th>Nombre</th> 
          <th>Apellido</th> 
          <th>Telefono</th> 
          <th>Email</th> 
          <th>Dirección</th> 
         </tr> 
         </thead> 
         <tbody> 

         </tbody> 
        </table> 
       </div> 

      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button> 
      </div> 
     </div> 
    </div> 
</div> 

模態的鈕

​​

路線

Route::post('admin/proveedores/item', [ 
    'as' => 'admin.proveedores.item', 
    'uses' => '[email protected]']); 

控制器的功能

public function item(Request $request) 
{ 
    $items = Proveedores::select($request->id); 

    return json_encode($items); 
} 

main.js1 main.js2

我測試的是一個和其他人,但我得到它顯示我在控制檯一個空對象

回答

1

首先,在你的JavaScript你傳遞的id作爲proveedores_id最大,但在您的控制器中,您嘗試使用$request->id訪問它。

這可能是一個想法,看看https://laracasts.com/series/laravel-from-scratch-2017/episodes/8

其次,只要使用select你只是將要返回的Builder一個JSON編碼版本。

爲了讓您的請求以實際返回的Proveedores一個實例,你會做這樣的事情:

public function item(Request $request) 
{ 
    $item = Proveedores::findOrFail($request->id); 

    return compact('item'); 
} 

這也意味着你可以刪除for循環您的成功方法內,簡單地response.item.*例如訪問數據

function (response) { 

    console.log(response) 

    table.html('') 

    var fila = "<tr>" + 
      "<td>" + response.item.name + "</td>" + 
      "<td>" + response.item.last_name + "</td>" + 
      "<td>" + response.item.tel + "</td>" + 
      "<td>" + response.item.address + "</td>" + 
      "</tr>"; 

    table.append(fila); 

} 

希望這有助於!

+0

find和findorfail和有什麼區別? – basosneo

+0

@basosneo如果find'不存在,find'將返回'null'。 'findOrFail()'會拋出一個異常,最終會變成404響應。 https://laravel.com/docs/5.3/eloquent#retrieving-single-models –

+0

因爲我使用'find'工作,因爲'findorfail'不工作 – basosneo