【问题标题】:Newly created Eloquent object won't return attribute in separate create Eloquent call新创建的 Eloquent 对象不会在单独的 create Eloquent 调用中返回属性
【发布时间】:2015-11-24 10:47:10
【问题描述】:

下面的代码显示了我的 Eloquent Link 类中的一个方法。创建一个新的Link 后,我想创建一个相关的对象LinkContact,然后用新的Link 创建一个associate()

创建LinkContact 时,我在访问Link 的id 属性时遇到问题。只有在为新的相关对象 (LinkContact) 执行 create 方法时,Link id 值才会显示为不可访问。

在调用 create 方法之前使用 Laravel Debugbar 一行我已经记录了 Link id,我可以看到它很好!任何想法为什么会发生这种情况以及为什么我无法访问该值?!和范围有关吗?

请注意cmets在哪里

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_id 在我的可填充属性中:

class LinkContact extends Model
{
    protected $fillable = ['type', 'link_id', 'institution_contact_id'];

    // rest of the class
}

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    值得注意的是:

    $newLink->contacts()->associate($linkContact);
    

    在这种情况下,会运行如下查询:

    UPDATE `link_contacts` SET `link_id` = 1 WHERE `id` = 4;
    

    因此,如果您在创建中设置link_id,则无需运行关联。如果没记错的话,您可以运行$newLink->contacts()->create([...]) 而不是create()associate()

    由于 Eloquent 的工作方式,始终会通过运行旨在自动插入此数据的任何辅助方法来更新/插入外键,但您手动指定键,这意味着它受 $fillable 的约束,所以我会看一下,如果您目前没有它,请将 link_id 添加到该数组中。

    【讨论】:

    • 感谢您的评论 ollieread,$newLink->contacts()->create([...]) 是我最初所做的,但是,当我在 artisan tinker 中运行此代码之前,如果我运行 $newLink->contacts,新创建的相关模型不会显示我认为associate() 的目的是让$newLink->contacts 显示新添加的关系。也许我理解错了,或者我之前的测试不正确?
    • 好吧,它不会出现在$newLink->contacts 中,因为在您访问它时它没有被加载。但是对于之后的任何请求都应该这样做。
    • 对,我看看能不能访问到方法返回的数据中的关系。
    • ollieread,你是老板 - 你是对的,我现在可以在从方法返回数据后访问关系。我猜如果我在添加后需要直接访问关系,使用$newLink->load('contacts') 是否正确?
    • 您可以使用load,但这会创建对数据库的不必要调用。您可以手动将其添加到集合中,我现在不记得该怎么做,但这会更好。
    【解决方案2】:

    试试

    $newLink->save();
    

    打电话之前

    $newLink->id
    

    id 是在数据保存到数据库后才生成的,所以在此之前无法知道它。

    【讨论】:

    • 感谢您的评论乔治。不争论你在说什么,我会试一试,但是在不可用的方法之前,值怎么可能在一行代码中可用?!另外,据我所知,自动创建将保存到数据库中,因此该值应该可用,并且我确信我之前已经在我的代码中做到了这一点,并且我从来没有在我说过的地方以雄辩的方式运行代码,例如, ->create([$attr]) 然后说->save()
    • 我认为这不可能。也许如果您编写一个创建链接并返回链接 id 的函数,然后在此 foreach 中将其传递给您的 linkcontact 结构?
    • 按照你的建议,我在create($attr) 之后运行了save(),但我得到了同样的错误。
    • Create是保存UPDATE的INSERT版本,所以id肯定会在create之后设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2017-04-18
    • 2018-07-02
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多