2016-08-11 181 views
0

我有一个页面显示我的所有客户。它使用paginate,每页仅显示16个客户端。因此我提供了实时搜索功能。显示搜索范围内的结果

当搜索是执行,从结果选择的选项触发以下

select: function (event, ui) { 
    $.ajax({ 
     url: "/returnClient", 
     type: "GET", 
     datatype: "html", 
     data: { 
      value : ui.item.value 
     }, 
     success: function(data) { 
      $('.container').fadeOut().html(data.html).fadeIn(); 
     } 
    }); 
} 

即基本上调用下面的函数

public function returnClient(Request $request) 
{ 
    if($request->ajax()){ 
     $selectedClient = $request->input('value'); 
     $client = Client::where('clientName', $selectedClient)->first(); 

     $html = View::make('clients.search', $client)->render(); 
     return Response::json(array('html' => $html)); 
    } 
} 

如果我输出上述客户机变量,我可以看到这个特定客户的所有细节。然后这被传递给部分clients.search。 在clients.search,如果我做

{{dd($client)}} 

我得到了一个未定义的变量:客户端。为什么它没有在视图中得到解析对象?

非常感谢

+0

不确定您是否真的在这里使用API​​。我认为更好的方法是在视图内渲染HTML,然后API将调用端点并接收一些数据,并通过Javascript在视图内填充此数据。传递完整的HTML将会破坏API的想法。 –

回答

3

的问题是,你是不正确传递$client到视图。 Views documentation显示了如何通过关联数组正确传递数据。 API docs确认数组是预期的。

而是执行此操作:

public function returnClient(Request $request) 
{ 
    if($request->ajax()){ 
     $selectedClient = $request->input('value'); 
     $client = Client::where('clientName', $selectedClient)->first(); 

     $html = View::make('clients.search', ['client' => $client])->render(); 
     return Response::json(array('html' => $html)); 
    } 
} 

此外,作为习惯的时候你可能要考虑使用的dump()代替dd()

+0

对于其他信息:'dump()'是函数'dd()'在幕后调用的函数,因此它基本上是相同的东西,而不会死亡并且不接受可变数目的参数。 – samrap