通过 ajax 从 Laravel 4 中的 db 加载数据到视图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17774891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load data into view via ajax from db in Laravel 4
提问by SteB
So far I have a User view that contains a typehead input box and a button.
到目前为止,我有一个包含打字头输入框和按钮的用户视图。
I can manage to get the id of the selected user and display it when clicking on the button:
我可以设法获取所选用户的 id 并在单击按钮时显示它:
$('#load').bind('click', function(e)
{
alert(selected);
});
Now I want to retrieve the data for the selected user from the database and display it in the User view. Here is my jQuery, route and controller:
现在我想从数据库中检索所选用户的数据并将其显示在用户视图中。这是我的 jQuery、路由和控制器:
var details = '<?php echo URL::to('details'); ?>';
$.getJSON(
details,
function(data)
{
$('#details').html(data);
}
);
Route::get('details', 'HomeController@Details');
public function Details()
{
$data = array('name' => 'Ste');
return View::make('user')->nest('details', $data);
}
I've read multiple articles of AJAX in Laravel but am no closer to getting this working.
Does anyone know of a good tutorial for doing this or am I doing something obviously wrong?
我在 Laravel 中阅读了多篇关于 AJAX 的文章,但离让它工作更近一步了。
有没有人知道这样做的好教程,或者我做错了什么?
回答by SteB
Took me a while to figure out, but this is what I've ended up with.
This simply returns some html constructed in a view from a database query based on a single value (userid) passed into a controller method via jQuery ajax.
Eg: The user uses Bootstrap Typeahead to select a user, then (via ajax) gets the user's details from the database and displays then.
我花了一段时间才弄清楚,但这就是我最终得到的。
这只是根据通过 jQuery ajax 传递给控制器方法的单个值(用户 ID)从数据库查询返回一些在视图中构造的 html。
例如:用户使用 Bootstrap Typeahead 选择一个用户,然后(通过 ajax)从数据库中获取用户的详细信息并显示出来。
The AJAX:
阿贾克斯:
var details = '<?php echo URL::to('details'); ?>';
$.ajax(
{
url: details,
type: 'GET',
dataType: 'html',
data: {id: selected},
}).done(
function(data)
{
$('#details').html(data);
}
);
The Controller:
控制器:
public function Details()
{
$id = Input::get('id');
$user = DB::table('Users')->where('UserID', $id)->get();
$data = array('user' => $user);
return View::make('details', $data);
}
The View:
风景:
@foreach($user as $person)
<label class="key">User ID</label>
<label class="data">{{ $person->UserID }}</label>
<label class="key">Person</label>
<label class="data">{{ $person->FullName, $person->Email, $person->WebSite }}</label>
@endforeach
Notes:
笔记:
- All my views are blade templates.
- My details view cannot inherit from another template (no extends('master')), this stops the view from working (no idea why).
- I'll only ever be returning 1 record, I use @foreach to access the array and turn each element into an object (there may be a better way of doing this).
- 我所有的观点都是刀片模板。
- 我的详细信息视图不能从另一个模板继承(没有扩展('master')),这会阻止视图工作(不知道为什么)。
- 我只会返回 1 条记录,我使用 @foreach 访问数组并将每个元素转换为一个对象(可能有更好的方法)。
回答by Blue Genie
When you're using geJson you're expecting a json response, so you'll nee to use $.ajax() instead.
当您使用 geJson 时,您期望得到 json 响应,因此您需要改用 $.ajax()。
Another solution is to return
另一个解决方案是返回
return Response::json(['view' => View::make('user')->nest('details', $data)]);
And then
进而
$.getJSON(
details,
function(data)
{
$('#details').html(data.view);
}
);
回答by Dammy
Just to add to what SteB is saying. To not use @foreach, if you only have/need 1 record, you should do this instead:
只是为了补充 SteB 所说的内容。要不使用@foreach,如果您只有/需要 1 条记录,您应该这样做:
$user = DB::table('Users')->where('UserID', $id)->first();
and then in your view you should be able to reference $user directly and not need a @foreach loop. This is a better way since it's one record you are retrieving.
然后在您看来,您应该能够直接引用 $user 而不需要 @foreach 循环。这是一种更好的方法,因为它是您正在检索的一个记录。