当试图访问laravel控制器中对象中的数组时,无法访问字符串类型的字符串偏移量



我有点困惑,因为我已经尝试了我所理解的关于在对象中获取数组中的项。

让我打破

在我的客户端端点

$client=Client::where('id',$client)->firstOrFail();
$arr = json_decode($client->attributes);
return response()->json($arr);

当我像这样返回

return response()->json($client->attributes);

{
"full_details_acknowledgement": "10",
"offer_letter_acknowledgement": "10",
"offer_letter": "10",
"offer_letter_variables": [
"basic_salary",
"housing_allowance",
"transport_allowance",
"meal",
"entertainment",
"hazard_allowance",
"leave_allowance",
"utility",
"monthly_gross_salary",
"statutory_deductions",
"employee_pension",
"payee_tax",
"total_deductions",
"net_monthly_salary",
"austin"
],
"company": "global-manpower"
}

我试图得到offer_letter_variables的值,并在一个变量

中保护它们像这样,这也是我尝试过的

foreach ($client->attributes['offer_letters_variables'] as $variable){

$offer_letters_variables->put($variable,isset($request->{$variable}) ? $request->{$variable} : 0 );
}

但是如果我像上面那样尝试,就会出现错误

"message": "Cannot access offset of type string on string"

这里是我的代码的完整视图(我注释掉了一些部分)

public function submitSingleUploadCandidates($client,Request $request){

$request->validate([
'job_role_id'=>'required',
'mail_template_id'=>'required',
'first_name'=>'required',
'last_name'=>'required',
'user_type'=>'required',
'email'=>'required',
]);

$job_level=JobLevel::find($request->job_level_id);
$job_role=JobRole::findOrFail($request->job_role_id);
$mail_template=MailTemplate::findOrFail($request->mail_template_id);
$client=Client::where('id',$client)->firstOrFail();
//return response()->json($client->attributes);
// $arr = json_decode($client->attributes);
//dd($client);
// return response()->json(gettype($arr));
// return response()->json($arr);
$offer_letters_variables=collect([]);

//return response()->json($offer_letters_variables);

// $var = $client->attributes[''];

// dd($var);


foreach ($client->attributes['offer_letters_variables'] as $variable){

$offer_letters_variables->put($variable,isset($request->{$variable}) ? $request->{$variable} : 0 );
}
$attr=collect(['offer_letter_variables'=>$offer_letters_variables]);
$user=User::where('email',$request->email)->first();


if ($user){
Session::flash('fail', 'Candidate with email already exist');
$payload=['status'=>'fail','details'=>'Candidate with email already exist'];
return response()->json($payload, 200);
return  redirect()->back()->withInput();
}
$password=Str::random(7);
$job_level_id = $job_level->id ?? null;
$new_user=User::create([
'client_id'=>$client->id,
'email'=>$request->email,
'emp_num'=>$request->emp_num,
'first_name'=>$request->first_name,
'last_name'=>$request->last_name,
'stage_id'=>1,
'user_type'=>$request->user_type,
'job_level_id'=>$job_level_id,
'job_role_id'=>$job_role->id,
'attributes'=>$attr,
'password'=>Hash::make($password),
]);
// $mail_constants['MacTay Signature Banner'] = '';
$mail_constants = $this->getMailConstants($new_user);
$mail_constants['candidate_password']=$password;
$mail_constants['deadline']=Carbon::now()->addWeekdays(2)->format('D d M, Y');
$mail_constants['admin_name']=auth()->user()->name;
$mail_content=$this->convertMailTemplateToEmail($mail_template,$mail_constants);
$mail_template->subject = str_replace('{{job_role}}', $mail_constants['job_role'], $mail_template->subject);
$mail_template->subject = str_replace('{{client_name}}', $mail_constants['client_name'], $mail_template->subject);
Mail::to($new_user->email)->send(new AdminSendMail($mail_content,$mail_template->subject));
$message="Your account has been created on Mactay App. Email: {$new_user->email}, Temp Password: {$password}. URL: onboarding.mactay.com";
SendSMSJob::dispatch($new_user->phone,$message);
activity()->withProperties(['client_id' => $client->id])->log('Upload single candidate to '.$client->name);
Session::flash('success', 'Successfully Uploaded Single Candidates Details');

$payload=['status'=>'success','details'=>'Successfully Uploaded Single Candidates Details'];
return response()->json($payload, 200);
}

请我做错了什么,请帮忙,提前感谢

您忘记json_decode$client->attributes

$clientAttributes = json_decode($client->attributes);
foreach ($clientAttributes->offer_letter_variables as $variable){
$offer_letters_variables->put(
$variable,
$request->{$variable} ?? 'not set'
);
}
$attr = collect(['offer_letter_variables' => $offer_letters_variables]);


如果你想像数组一样访问它,你可以像关联数组一样json_decode这个值

$clientAttributes = json_decode($client->attributes, true);
dd($clientAttributes['offer_letter_variables']);


也不是你在foreach循环中将offer_letter_variables拼错为offer_letters_variables

您将得到这样的offer_letter_variables

$offerLetters = 0;
$client=Client::where('id',$client)->firstOrFail();
if(isset($client->attributes['offer_letter_variables'])){
$offerLetters = $client->attributes['offer_letter_variables'];
}

是否需要使用json_decode的第二个参数?记住,如果它是一个关联数组

$client=Client::where('id',$client)->firstOrFail();
$arr = json_decode($client->attributes);

什么返回gettype() ?数组?

多亏了@maazin,解决方案是使用json_decode $client->attributes,然后像这样使用foreach

$clientAttributes = json_decode($client->attributes);
foreach ($clientAttributes->offer_letter_variables as $variable){
$offer_letters_variables->put(
$variable,
$request->{$variable} ?? 'not set'
);
}
$attr = collect(['offer_letter_variables' => $offer_letters_variables]);

最新更新