我試圖從請求中檢索數據,因此我將$data = $request->only('user_id', 'clientParticipants', 'event_type','contactParticipants', 'schedule', 'stellarParticipants', 'summaries', 'type', 'venue', 'with_client')
放置在以下格式中,這是在Laravel中執行dd($ data)時的格式:如何在json中獲取數組數據或獲取laravel中的數據屬性
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"
]
我想檢索彙總所以爲了這個,我正在嘗試做以下:
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);
}
我得到的錯誤,而在摘要中檢索任何數據:
試圖讓非對象的屬性
更新 我InteractionSummary模式是:
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();
});
}
和互動有如下關係:
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function meetingSummaries()
{
return $this->hasMany('App\InteractionSummary');
}
你爲什麼這樣做''上$ summary' json_encode'?這將生成一個字符串。 – apokryfos