Laravel在一对一关系上更新或创建 [英] Laravel updateOrCreate on OneToOne relationShip

查看:642
本文介绍了Laravel在一对一关系上更新或创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我有以下模型:

in my web application i have this models:

InstagramAccount.php 
UserPageFeed.php

每个InstagramAccount在UserPageFeed中都有一个记录,而每个UserPageFeed在InstagramAccount中都属于一个记录,那么这就是one to one关系,

each InstagramAccount has one record into UserPageFeed and each UserPageFeed belongs to one record into InstagramAccount, then that's one to one relationship,

问题:

我下面的代码无法更新表上的现有行并再次创建

my below code couldn't update existing row on table and create again

$userSelectedPage = InstagramAccount::whereUsername('my_page')->first();
$userPageFeeds = new UserPageFeed();
$userSelectedPage->account()->updateOrCreate([
    'instagram_account_id' => $userPageFeeds->id, //exsiting row
    'page_name' => 'test',
    'feeds' => 'test',
    'cache_time' => Carbon::now()->addHour(6),
]);

或此代码:

$userSelectedPage = InstagramAccount::whereUsername('content.world')->first();
$salam = $userSelectedPage->account()->updateOrCreate([
    'instagram_account_id' => $userSelectedPage->id,
    'page_name' => 'aaaaaaa',
    'feeds' => 'ddd',
    'cache_time' => Carbon::now()->addHour(6),
]);

user_page_feeds表结构:

user_page_feeds table structure:

id                     ->Primary
instagram_account_id   ->Index
feeds
page_name
cache_time
created_at
updated_at 

具有以下索引:

"Keyname":user_page_feeds_instagram_account_id_foreign  "Column":instagram_account_id

instagram_accounts表结构:

instagram_accounts table structure:

id                     ->Primary
user_id                ->Index
uid
fid
proxy
avatar
username
password
checkpoint
account_data
people_data
status
created_at
updated_at 

InstagramAccount型号:

class InstagramAccount extends Model
{
    protected $guarded = ['id'];

    protected $casts = [
        'account_data' => 'array',
        'people_data' => 'array'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function account()
    {
        return $this->hasOne(UserPageFeed::class);
    }
}

UserPageFeed型号:

class UserPageFeed extends Model
{
    public $incrementing = false;
    protected $guarded = ['id'];
    protected $casts = [
        'feeds' => 'array'
    ];

    public function account()
    {
        return $this->belongsTo(InstagramAccount::class,'instagram_account_id');
    }
}

推荐答案

您必须将updateOrCreate()与两个单独的参数一起使用:

You have to use updateOrCreate() with two separate parameters:

$userSelectedPage->account()->updateOrCreate(
    ['instagram_account_id' => $userPageFeeds->id],
    [
        'page_name' => 'test',
        'feeds' => 'test',
        'cache_time' => Carbon::now()->addHour(6),
    ]
);

第一个参数包含Laravel用于查找现有属性的属性 account.
第二个参数包含Laravel用于创建或更新account的属性.

The first parameter contains the attributes that Laravel uses to find the existing account.
The second parameter contains the attributes that Laravel uses to create or update the account.

这篇关于Laravel在一对一关系上更新或创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆