3
AngularJs를 Laravel과 함께 사용하는 방법을 배우려고합니다. 나는 많은 혀를 읽고 봤지만 나는 라라벨 (Laravel) 관계를 사용하기 위해 어떤 것을 찾을 수 없었다. 내 게시물과 저자 모델간에 belongsTo 많은 관계가 있습니다. 블레이드를 사용할 때 저자 이름을 사용할 수 있습니다 {{$post->author->name}}
AngularJS belongsTo-hasMany 관계
이제 angularjs를 얻으려고합니다. JS 파일로
class Post extends Eloquent {
public function author()
{
return $this->belongsTo('Author');
}
}
class Author extends Eloquent {
public function posts()
{
return $this->hasMany('Post');
}
}
class UserController extends BaseController {
public function getIndex(){
$posts = Post::where('id','<','10')->get();
return Response::json($posts);
}
}
:
// public/js/controllers/mainCtrl.js
angular.module('mainCtrl', [])
.controller('mainController', function($scope, $http, Post) {
$scope.postsData = {};
// loading variable to show the spinning loading icon
$scope.loading = true;
Post.get()
.success(function(data) {
$scope.posts = data;
$scope.loading = false;
});
//dont know how to get author here in foreach method
angular.forEach($scope.posts,function(post)...
});
// public/js/services/commentService.js
angular.module('postService', [])
.factory('Post', function($http) {
return {
// get all the posts
get : function() {
return $http.get('/api/posts/');
}
}
});
<div class="comment" ng-hide="loading" ng-repeat="post in posts">
<h3>Comment #{{ post.id }} </h3> <p>{{post.title}}</p>
{{post.author_id}}/{{post.author.name}}
</div>
나는 가까운 솔루션하지만 여전히 혼란 내가 포스트 서비스에 저자 이름을 얻는 방법입니다. 어떤 도움을 주시면 감사하겠습니다.
감사
사용자 컨트롤러에서