作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试与 Laravel 建立多态关系。我已阅读文档并查看相关代码,但似乎无法使其全部正常工作。我正在尝试将照片
附加到房间
,其中一个房间可以有很多照片。与关系关联的type
有效,但id
始终为0
。
为什么其中一部分会起作用,有人可以指出我的关系中存在哪些问题吗?
我认为问题与我在表中创建数据的方式有关:
$photo = new Photo;
$photo->URL = 'thePhotoURL.extension';
$photo->save();
$room = new Room;
$room->name = 'Some Room Name';
// seems weird to me that I "save" the photo twice
// or am I just saving the relationship here?
// have tried the other relationship options too (associate, attach, synch)
$room->photos()->save($photo);
// have also tried:
// $room->photos()->save(new Photo(array('URL' => 'urlTest.com')));
// get error "URL"
$room->save();
创建照片表:
public function up() {
Schema::create('photos', function($t) {
$t->increments('id');
// ... ...
// the Id is always 0 but the type is correct
$t->integer('imageable_id');
$t->string('imageable_type');
$t->softDeletes();
$t->timestamps();
});
}
还有我的类(class),
照片:
class Photo extends Eloquent {
public function imageable() {
return $this->morphTo();
}
}
房间:
class Room extends Eloquent {
public function photos() {
return $this->morphMany('Photo', 'imageable');
}
}
我发现保留字 ( like Event ) 存在问题,但这似乎不是这里的问题。
最佳答案
保存原始文件后,您需要运行$room->photos()->save($photo);
。原始数据尚不存在,因此没有可关联的 ID。
也许它应该抛出异常,也许不应该。就我个人而言,我宁愿它对我尖叫。
关于Laravel-4 多态关系仅分配类型(而不是 ID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18699835/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!