如何使用ajax检索数据?我在一些项目中使用了我的ajax代码来从数据库检索记录,但不知道如何在laravel 5中使用它,因为它有路由和控制器。
我有这个HTML:
<select name="test" id="branchname">
<option value="" disabled selected>Select first branch</option>
<option value="1">branch1</option>
<option value="2">branch2</option>
<option value="3">branch3</option>
</select>
<select id="employees">
<!-- where the return data will be put it -->
</select>
还有ajax
$("#branchname").change(function(){
$.ajax({
url: "employees",
type: "post",
data: { id : $(this).val() },
success: function(data){
$("#employees").html(data);
}
});
});
在我的控制器中,我声明了两个Eloquent模型。模型1用于branchname表,模型2用于employees表。
use App\branchname;
use App\employees;
所以我可以像以下这样检索数据。
public function getemployee($branch_no){
$employees = employees::where("branch_no", '=', $branch_no)->all();
return $employees;
}
如何返回我从员工表中提取的记录?从路由连线到控制器并将响应返回给ajax post请求?任何帮助、建议、推荐、想法、线索都将不胜感激。谢谢!
PS:我是Laravel 5的新手。
$.post(url+'/'+$(this).val(), function(response){...});
。 - The Alpha$.each(response.employees, function(i, employee){...})
中使用employee.id
和employee.title
,我犯了一个错误 :p - The Alpha