- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习将 AngularJs 与 Laravel 结合使用。我已经阅读和观看了许多 tuts,但我找不到任何使用 Laravel 关系的内容。我的帖子和作者模型之间有 belongsTo-many 关系。使用 Blade 时,我可以使用 {{$post->author->name}}
现在我正在尝试获取 angularjs。
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);
}
}
使用 js 文件:
// 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>
我已接近解决方案,但仍然对如何在 Post 服务中获取作者姓名感到困惑。任何帮助表示赞赏。
谢谢
最佳答案
在您的用户 Controller 中,在检索帖子时预先加载作者,这将允许通过 post.author
以 Angular 访问它
public function getIndex(){
$posts = Post::with('author')->where('id','<','10')->get();
return Response::json($posts);
}
关于javascript - AngularJS belongsTo-hasMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22634061/
是否可以在一个集合/json 中获取所有详细信息(用户、帖子、postimages)? 用户->hasmany(帖子) 后->hasmany(后图像) 用户: 身份证 |姓名 邮政: 身份证 |用户
我正在制作一个具有基本消息传递功能的 PHP/MySQL Web 应用程序。两个用户将能够进行包含一系列消息的“对话”。 以下是我设想的模型关系: 用户> 多对多> 对话 对话> 多对多> 用户和一对
我的问题很简单: 我有一个名为users 的表。这些用户可以有很多联系人。这些联系人是其他用户。 所以我有一个名为 userHasContacts 的表,其中包含所有者的 ID (userID) 和联
我有两个 GORM 域类。一个是卡,卡上有很多购买。有什么方法可以搜索大于或等于某些购买的卡。这可能是一个例子 我们有4张牌: Card1:橙、橙、苹果 Card2:苹果、橙子 Card3:橙色,橙色
背景: 我有三个表,即生产、生产线和操作。 生产线引用其先前的生产线,列名为“parent_id”。生产线的深度应等于操作的深度。 以下是他们的关系: Production_lines has man
我有两个表(项目、任务)。 项目表结构如下: id - title - desc - others_column 1 - title - desc - others 2 - title - des
提前感谢您的帮助。 我有两个模型菜单和窗口及其关联 db.menus.hasMany(db.windows); db.windows.belongsTo(db.windows); 我正在尝试使用与此类
使用hasMany有区别吗 static hasMany = [objects:Object] 并添加对象的集合 Set objects 最佳答案 使用之间绝对有区别: static hasMany
我有这四个类: class Animal { static hasMany = [legs: Leg] } class Cat extends Animal { static hasM
当我尝试创建与其父模型关联的子模型时遇到问题。 我有 2 个模型: 机构(有很多地址) 地址(有 1 个机构) 当我单击一个机构并转到 show.blade.php 时,我有一个表单可以为该机构创建地
我正在运行 cakephp 2.0.2,我有以下关系 Category hasMany CategoryWork Work hasMany CategoryWork Categ
是否有一个简单的解决方案可以通过模型上的 hasMany Relation 分离(不删除)所有相关模型? 例如,我有两个表: 学院(ID、名称) 学生(id、姓名、college_id(可为空)) 在
Ember Data 是否类似于 Rails 的 has_many ..., :through =>功能? 最佳答案 它不是。在 1.0 Beta 中添加了一个 Unresolved 问题。里程碑:M
我有用户表,然后是他们的兴趣表。 最好是通过表来管理 hasMany 来管理两者之间的关系,还是最好将 CSV 写入“用户”表中的一列。 到目前为止,我一直在通过表格使用 hasMany,但想知道最好
假设我有两个模型:Park 和 Items。通过 HasMany 关系,每个 Park 都可以有多个 Items。 在 Items 表中有一个 park_id 字段和一个 type 标志:一个 Ite
这是save和 saveMany HasMany 上的方法关系类,但 dissociate(Many) 在哪里?/detach(Many)方法?也没有获得逆关系方法的内置方法,那么从 HasMany
我试图让我在 CakePHP 2.3 中的关联模型正确保存,但我遇到了问题。我正在存储帖子,我想知道这些帖子中有哪些链接。对于这些链接中的每一个,如果可用,我想存储 anchor 文本。我的数据库设置
我有两个类(例如简化): public class Data { public int Id {get; set;} public string Value { get; set; }
有域对象: class Book { List pictures static hasMany = [pictures:Picture] static mappi
我正在尝试实现 HasMany与不可为空的键列的映射。 我的映射如下所示: public void Override(AutoMapping mapping) { mapping.Table(
我是一名优秀的程序员,十分优秀!