Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialIsaiah Curry
3,950 PointsGet Data From Laravel Route with Angularjs
I am trying to get the data from my laravel Route with Angular
URL: http://test.dev/todos/show/141
Route::get('todos/show/{id?}', function($id=null)
{
$users = DB::table('customers')->where('id', $id)->get();
return $users;});
Angular
<script type="">
function SimpleController($scope, $http) {
$http.get('/todos/show/{id}').success(function(data){
$scope.customers = data;
})}
</script>
<div class="container">
<h3>Adding a Simple Controller</h3>
<ul>
<li ng-repeat="cust in customers | filter:name ">
@{{ cust.customerName }} - @{{ cust.customerNumber }}
</li>
I get nothing back, unless I manually add the id 141 to to my query ->where('id', 141). Could someone please tell me a method I could user to do this? Do I have to use $resource? Thanks
2 Answers
Chris Shaw
26,676 PointsHi Isaiah,
You have a couple of issues to solve before your request will work and they are as follows.
- In your ajax request you're not passing a valid id, instead you just have
{id}
which will be parsed as a string - Your request isn't returning a JSON response which is the most common data type to return from a request
JavaScript
// You need to pass the id here so that your backend request handler can remain dynamic
$http.get('/todos/show/141').success(function(data) {
$scope.customers = data;
});
PHP
<?php
Route::get('todos/show/{id?}', function($id = null)
{
$users = DB::table('customers')->where('id', $id)->get();
return json_encode($users);
});
Hope that helps.
Isaiah Curry
3,950 PointsThanks for the tips and the quick response! I'll try this out.