简体   繁体   English

Laravel 属于关系返回 null

[英]Laravel belongsTo relationship returns null

I'm building a rudimentary CRM app using Laravel 6.0.我正在使用 Laravel 6.0 构建一个基本的 CRM 应用程序。 Users can freely create accounts, but to get any functionality out of the app, they need to set up a SubscriptionAccount (or join an existing one), which will then allow them to create/manage customer Accounts, add Users, etc. (each is a one to many).用户可以自由创建帐户,但要从应用程序中获取任何功能,他们需要设置一个 SubscriptionAccount(或加入现有帐户),然后允许他们创建/管理客户帐户、添加用户等(每个是一对多)。

The User model's relationship to SubscriptionAccount model is giving me issues.用户模型与 SubscriptionAccount model 的关系给我带来了问题。 For example:例如:

$user = User::find(1);
$user->subscription()->create(['name' => 'Test Subscription']);
$user = $user->fresh();
dd($user->subscription); // returns null

I suspected it had to do with the belongsTo relationship in the User model, but the odd thing is that it actually creates and persists a new SubscriptionAccount while using that relationship (second line above), though if you access users relationship from the new SubscriptionAccount it also returns null.我怀疑它与用户 model 中的belongsTo关系有关,但奇怪的是它实际上在使用该关系(上面的第二行)时创建并保留了一个新的 SubscriptionAccount,但如果你从新的 SubscriptionAccount 访问users关系,它还返回 null。

Here are the models:以下是模型:

// User.php
class User
{
    public function subscription()
    {
        return $this->belongsTo(SubscriptionAccount::class, 'subscription_account_id');
    }
}

// SubscriptionAccount.php
class SubscriptionAccount extends Model
{
    public function users()
    {
        return $this->hasMany(User::class, 'subscription_account_id');
    }
}

The only thing out of the ordinary is shortening the name of the relationship to subscription from SubscriptionAccount , but that should have been taken care of by specifying the foreign key in both relationships.唯一不同寻常的是从SubscriptionAccount缩短到subscription关系的名称,但这应该通过在两个关系中指定外键来处理。 Here's the migrations:这是迁移:

Schema::create('subscription_accounts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->uuid('uuid')->unique();
    $table->string('name');
    $table->timestamps();
});

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->uuid('uuid')->unique();
    $table->bigInteger('subscription_account_id')->unsigned()->index()->nullable();
    $table->string('name');
    ...
    $table->timestamps();
    $table->foreign('subscription_account_id')
        ->references('id')
        ->on('subscription_accounts');
});

If I create the user from a SubscriptionAccount (ie $subscriptionAccount->users()->create([...]); it sets the correct subscription_account_id on the users table, but doesn't work vice versa.如果我从 SubscriptionAccount 创建用户(即$subscriptionAccount->users()->create([...]);它会在users表上设置正确的subscription_account_id ,但反之亦然。

This is a known issue (feature?) with the belongsTo relationship:这是belongsTo关系的一个已知问题(功能?):

https://github.com/laravel/framework/issues/29978 https://github.com/laravel/framework/issues/29978

To work around it you can associate the models manually:要解决此问题,您可以手动关联模型:

$user = User::find(1);
$sub = Subscription::create(['name' => 'Test Subscription']);
$user->subscription()->associate($sub);
$user->save();

So instead of using belongsTo because a subscription account does not belongs to one user, it can belong to many, you might want to use the hasOne relationship instead:所以不要使用belongsTo ,因为订阅帐户不属于一个用户,它可以属于多个用户,您可能希望使用hasOne关系:

public function subscription()
{
    return $this->hasOne(SubscriptionAccount::class, 'id', 'subscription_account_id');
}

It will belongTo one User if you had a user_id within the subscription_accounts table.如果您在subscription_accounts表中有一个user_id ,它将belongTo一个用户。

Let me know if it makes sense and if it works:)让我知道它是否有意义以及是否有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM