Php 新创建的雄辩对象赢得';在单独的create-elount调用中返回属性
下面的代码显示了我雄辩的Php 新创建的雄辩对象赢得';在单独的create-elount调用中返回属性,php,laravel,eloquent,Php,Laravel,Eloquent,下面的代码显示了我雄辩的链接类中的一个方法。在创建一个新的链接后我想创建一个相关的对象LinkContact,然后使用新的链接 创建LinkContact时,我在访问Link的id属性时遇到问题。只有在为新的相关对象(LinkContact)执行创建方法时,Linkid值才显示为不可访问 在调用create方法之前使用Laravel Debugbar一行,我已经记录了链接id,我可以很好地看到它!你知道为什么会发生这种情况,为什么我无法访问该值吗?!这与范围有关吗 请注意评论的位置 public
链接
类中的一个方法。在创建一个新的链接后
我想创建一个相关的对象LinkContact
,然后使用新的链接
创建LinkContact
时,我在访问Link
的id属性时遇到问题。只有在为新的相关对象(LinkContact
)执行创建方法时,Link
id值才显示为不可访问
在调用create方法之前使用Laravel Debugbar一行,我已经记录了链接
id,我可以很好地看到它!你知道为什么会发生这种情况,为什么我无法访问该值吗?!这与范围有关吗
请注意评论的位置
public function createActiveLink($students)
{
$links = [];
foreach ($students as $student) {
$newLink = $this->create([
'token' => $student->saudi_nid,
'status' => 'active',
'course_title' => $student->course_title,
'university_id' => $student->university_id,
'student_id' => $student->id,
'institution_id' => $student->institution_id,
'course_id' => $student->course_id,
]);
$studentContacts = $student->institutionContacts;
if ($studentContacts) {
foreach ($studentContacts as $studentContact) {
/* I get the value here, no problems */
\Debugbar::info($newLink->id);
$linkContact = $this->contacts()->create([
'type' => $studentContact->pivot->type,
'institution_contact_id' => $studentContact->pivot->institution_contact_id,
/* $newLink->id returns nothing here */
'link_id' => $newLink->id, here!!
]);
$newLink->contacts()->associate($linkContact);
$newLink->save();
}
}
$links[] = $newLink;
}
return $links;
}
尝试上述代码时收到的错误:
SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'link_id' cannot be null
(SQL: insert into `link_contacts`
(`type`, `institution_contact_id`, `link_id`, `updated_at`, `created_at`)
values (1, 1, , 2015-11-24 10:26:32, 2015-11-24 10:26:32))
C:\...\vendor\laravel\framework\src\Illuminate\Database\Connection.php#631
SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'link_id' cannot be null
C:\...\vendor\laravel\framework\src\Illuminate\Database\Connection.php#380
重申一下,我确实在Laravel Debugbar中得到了值,但在它之后的方法调用中没有
编辑:
在我的LinkContact
类中link\u id
在我的可填充属性中:
class LinkContact extends Model
{
protected $fillable = ['type', 'link_id', 'institution_contact_id'];
// rest of the class
}
试一试
打电话之前
$newLink->id
id仅在数据保存到数据库后生成,因此在此之前无法知道它。值得注意的是:
$newLink->contacts()->associate($linkContact);
在本例中,将运行如下查询:
UPDATE `link_contacts` SET `link_id` = 1 WHERE `id` = 4;
因此,如果您在create中设置链接id
,则无需运行associate。如果内存可用,您可以运行$newLink->contacts()->create([…])
而不是create()
和associate()
由于Eloquent的工作方式,外键总是会在运行任何旨在自动插入此数据的帮助器方法时被更新/插入,但您正在手动指定该键,这意味着它受
$filleble
的约束,因此我会看一看,如果您当前没有外键,将link\u id
添加到该数组。感谢您的评论。我不会反驳你说的话,我会试一试,但是这个值怎么可能在不可用的方法之前一行代码就可用呢?!另外,据我所知,create automatically将保存到数据库中,因此值应该是可用的,我确信我以前在我的代码中已经这样做过,而且我从来没有在我说过的有说服力的地方运行过代码,例如,->create([$attr])
,然后说->save()
。我认为这是不可能的。如果您编写一个函数来创建链接并返回链接的id,然后在此foreach中将其传递给您的linkcontact结构,那么可能会这样做?正如您所建议的,我在create($attr)
之后运行了save()
,但我得到了相同的错误。create是saves UPDATE的插入版本,所以id肯定会在创建之后设置。谢谢你的评论ollieread,$newLink->contacts()->create([…])
是我最初做的,当我在之前的artisan tinker
中运行此代码时,如果我运行$newLink->contacts
,新创建的相关模型不会显示,我认为associate()
的目的是允许$newLink->contacts
显示新添加的关系。也许我误解了,或者我之前的测试不正确?好吧,它不会出现在$newLink->contacts
中,因为在您访问它时它没有被加载。但是,对于之后的任何请求,都应该这样做。对,我将查看我是否有权访问该方法返回的数据中的关系。ollieread,你是老板-你是对的,我现在有权访问该方法返回的数据中的关系。我猜,如果我需要在添加关系后直接访问它,我是否可以正确地使用$newLink->load('contacts')
?您可以使用load
,但这会创建一个不必要的数据库调用。您可以手动将其添加到集合中,我现在不记得如何操作,但这样会更好。
UPDATE `link_contacts` SET `link_id` = 1 WHERE `id` = 4;