我正在尝试从请求中检索数据,所以我在{* 1}}放置以下是我在Laravel中执行dd($ data)时的格式:
$data = $request->only('user_id', 'clientParticipants', 'event_type','contactParticipants', 'schedule', 'stellarParticipants', 'summaries', 'type', 'venue', 'with_client')
我想检索摘要,为此我尝试执行以下操作:
array:10 [
"user_id" => 1
"clientParticipants" => array:2 [
0 => array:2 [
"label" => "Check 2 Contact - Test Company 5"
"value" => 4
]
1 => array:2 [
"label" => "Ammy Contact - Test Company 6"
"value" => 5
]
]
"event_type" => 2
"contactParticipants" => array:3 [
0 => array:2 [
"label" => "Check Contact - Test Company 3"
"value" => 2
]
1 => array:2 [
"label" => "Check 1 Contact - Test Company 2"
"value" => 3
]
2 => array:2 [
"label" => "Check 4 contact - Test Company 8"
"value" => 6
]
]
"schedule" => "2017-06-04 05:02:12"
"stellarParticipants" => array:1 [
0 => array:2 [
"label" => "Analyst"
"value" => 1
]
]
"summaries" => array:2 [
0 => array:5 [
"client" => array:2 [
"label" => "Test Company 4"
"value" => 7
]
"type" => "1"
"mention" => array:2 [
"label" => "Analyst"
"value" => 1
]
"action" => "Action Test 1"
"comment" => "Comment Test 1"
]
1 => array:5 [
"client" => array:2 [
"label" => "Test Company 5"
"value" => 8
]
"type" => "1"
"mention" => array:2 [
"label" => "Analyst"
"value" => 1
]
"action" => "Action Test 2"
"comment" => "Comment Test 2"
]
]
"type" => "Meeting"
"venue" => "Mumbai"
"with_client" => "0"
]
我在检索摘要中的任何数据时遇到错误:
尝试获取非对象的属性
更新 我的InteractionSummary模型是:
if($data['summaries'])
{
$container = [];
foreach($data['summaries'] as $summary)
{
$summary = json_encode($summary);
$user_id = $summary->mention['value'];
$container[] = new InteractionSummary([
'company_id' => $summary->client,
'nature' => $summary->type,
'user_id' => $user_id,
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
$interaction->meetingSummaries()->saveMany($container);
}
数据库架构:
class InteractionSummary extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'company_id', 'interaction_id', 'nature', 'user_id', 'action', 'feedback'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];
}
和互动有以下关系:
public function up()
{
Schema::create('interaction_summaries', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id')->index();
$table->integer('interaction_id')->index();
$table->string('nature');
$table->integer('user_id')->nullable();
$table->string('action')->nullable();
$table->string('feedback')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
答案 0 :(得分:1)
您正在使用json_encode
将数组转换为json字符串。从您的代码判断,您可能想要将数组转换为对象。
更改
$summary = json_encode($summary);
要
$summary = (Object)$summary;
同样$summary->client
返回一个数组。您可能打算使用$summary->client['value']
。
修改:试试这个
if($data['summaries']) {
foreach($data as $summary) {
$summary = (Object)$summary;
$interaction->meetingSummaries()->create([
'company_id' => $summary->client['value'],
'user_id' => $summary->mention['value'],
'nature' => $summary->type,
'action' => $summary->action,
'feedback' => $summary->comment
]);
}
}
答案 1 :(得分:0)
错误是json_encode()
函数始终返回一个字符串,并且您尝试将其作为对象访问。
此外,整个$data
变量已经是一个数组,因此您不需要对其元素实现任何与JSON相关的操作。
只需像普通数组一样使用它:
foreach($data['summaries'] as $summary)
{
$container[] = new InteractionSummary([
'company_id' => $summary['client']['value'],
'nature' => $summary['type'],
'user_id' => $summary['mention']['value'],
'action' => $summary['action'],
'comment' => $summary['comment']
]);
}