2017-06-04 39 views
1

我試圖從請求中檢索數據,因此我將$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'); 
} 
+0

你爲什麼這樣做''上$ summary' json_encode'?這將生成一個字符串。 – apokryfos

回答

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 
     ]); 
    } 
} 
+0

我得到一個錯誤'參數1傳遞給Illuminate \ Database \ Grammar :: parameterize()必須是類型數組,字符串給定,在Project_Folder \ vendor中調用\ laravel \ framework \ src \ Illuminaate \ Database \ Query \ Grammars \ Grammar.php在第681行並定義了' –

+0

分享您的InteractionSummary模型和表格架構。 – Sandeesh

+0

雖然即將出現同樣的錯誤,但我正在接受評論,反饋意見是我改變了。 –

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'] 
    ]); 
} 
+0

我收到一個錯誤,傳遞給Illuminate \ Database \ Grammar :: parameterize()的參數1必須是在Project_Folder \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Query中調用的類型數組,字符串\語法\ Grammar.php在線681和定義' –