从关系调用时未出现新创建的记录

时间:2018-12-11 11:18:14

标签: php laravel laravel-5

我有一个函数可以在称为“入职”的模型中创建新记录。然后,我想通过执行//takes array of functions like [fn1,fn2] // and returns a function: fn(arg)=>fn2(fn1(arg)) const compose = (functions = [(x) => x]) => (arg) => functions.reduce((result, fn) => fn(result), arg); const round = (decimals) => (x) => parseFloat(x.toFixed(decimals)); const percentage = (x) => x + '%'; const upperCaseName = (x) => x.slice(0, 1).toUpperCase() + x.slice(1); //formatters, key 'v1' will be rounded and made a percentage const formatters = { v1: [round(2), percentage], name: [upperCaseName], }; console.log( //data [ { name: 'john', address: 'a1', v1: 11.972646011733632 }, ].map(( o, //format each key that exist in formatters ) => Object.entries(o).reduce( (result, [key, value]) => ( //if compose is passed undefined it'll return x=>x function // so value will not be formatted (result[key] = compose(formatters[key])(value)), result ), {}, ), ), );在用户模型中通过“ hasOne”关系来调用此记录,但这返回的是null。以下是发生这种情况的功能片段:

auth()->user()->onboarding

我的商店功能:

public function apiSend(StoreOnboarding $request)
    {
        //Update the user's Onboarding details on DB
        $this->store($request);

        //Validate user's onboarding details
        $validation = $this->checkValidation(auth()->user()->onboarding);

}

我的商店功能正常运行,并且新的入职记录已添加到表中,但是在我的public function store(StoreOnboarding $request) { $onboarding = auth()->user()->onboarding()->updateOrCreate($request->validated()); } 函数中,出现错误:

“传递给App \ Http \ Controllers \ OnboardingController :: checkValidation()的参数1必须是App \ Onboarding的实例,给定为空”。

下面是我的apiSend函数的摘要:

checkValidation

这是我在用户模型中的入职关系:

public function checkValidation(Onboarding $onboarding)
{
    $validator = Validator::make($onboarding->toArray(), [
    //Validation here
    ]);
}

我不明白为什么public function onboarding() { return $this->hasOne(Onboarding::class, 'user_id', 'id'); } 不返回新创建的入职记录,而是返回null?

1 个答案:

答案 0 :(得分:0)

经过更多研究,我发现可以通过解决此问题

ServicePointManager.ServerCertificateValidationCallback = new        
RemoteCertificateValidationCallback(delegate { return true; });

在使用library(data.table) setDT(df) df <- melt(df, id.vars = c("ID", "Start_year")) # melt to long format df <- df[!is.na(value)] # remove NA entries df[, year := seq_len(.N) -1L, by = ID] # add year-number dcast(df, ID ~ year, value.var = "value") # reshape to wide format # ID 0 1 2 #1: 1 GBR GBR FRA #2: 2 FRA FRA <NA> #3: 3 GBR <NA> <NA> #4: 4 UKR RUS <NA>

之前

这将强制关系刷新。